Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript error: connectors does not accept the null parameter. #574

Open
Traveller23 opened this issue Nov 7, 2023 · 1 comment
Open

Comments

@Traveller23
Copy link
Contributor

Describe the bug
When I write the simplest example code in typescript, I get the following error:

1

import {useNode} from "@craftjs/core";
import Button from '@mui/material/Button';
export const MyComponent = () => {
    const {connectors: {connect, drag}} = useNode();
    return <Button ref={ref => connect(drag(ref))}/>;
}

2

TS2345: Argument of type  HTMLButtonElement | null  is not assignable to parameter of type
HTMLElement | ReactElement<any, string | JSXElementConstructor<any>>
Type  null  is not assignable to type
HTMLElement | ReactElement<any, string | JSXElementConstructor<any>>

I think the reason for this error is that the connector methods do not accept null as argument, but the ref can actually be null:

3

Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref).
See also: https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom

file: @types/react/index.d.ts

To Reproduce

Expected behavior

Screenshots

Additional context

Your environment

Software Version(s)
craft.js 0.2.2
React 18.2.0
TypeScript 5.2.2
Browser -
npm/Yarn -
Operating System -
@Traveller23
Copy link
Contributor Author

Indeed, I do know how to get around this problem, i.e. write drag(ref!) instead.
But I'm concerned that craft.js doesn't do anything special with null values, which could lead to problems that are currently invisible but could be far-reaching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant