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

Any way to make this work with hooks? #7

Open
3dGrabber opened this issue Aug 19, 2021 · 2 comments
Open

Any way to make this work with hooks? #7

3dGrabber opened this issue Aug 19, 2021 · 2 comments

Comments

@3dGrabber
Copy link

React's hook functions do not like to be called from within Builders.

I tried to mitigate this by eagerly instantiating ReactElements by appending ()s to builders everywhere, but to no avail.

@3dGrabber
Copy link
Author

Found out how.
It boils down to the different node types that are used in React.

There are Elements, Components (Function- and Class-) and this library throws Builders into the mix. Hooks (useState, etc.) work only in Function Components.

In order to use hooks in njsx you need to:

  • Define a function with a single input ("props") that returns a ReactElement.
    It cannot return a Builder. If you use Builders, don't forget the () at the end. Inside this function you may use hooks.
  • wrap that function using njsx(MyFuncComponent)

However I feel all this is cumbersome and goes against the "compositional" spirit of njsx.
IMO it's better to avoid hooks altogether when using njsx and use other means of state management.

@3dGrabber
Copy link
Author

leaving it open for now, in case someone has similar problems.

Shall I create a PR to add a section to the docs?

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