dsuryd / dotNetify

Simple, lightweight, yet powerful way to build real-time web apps.
https://dotnetify.net
Other
1.17k stars 164 forks source link

React 16.8 Hooks compatibility #177

Closed antonyrey closed 5 years ago

antonyrey commented 5 years ago

Hello,

Is it possible to use dotNetify with React's Hook-based components ?

Thank you, have a nice day

dsuryd commented 5 years ago

You can try and let me know :)

antonyrey commented 5 years ago

I tried but had no success, it looks like the dotnetify.connect method expects to receive a true "class-based component", to work... i wanted to use getState and setState options but an exception is thrown if i pass null as parameter value for the component

dsuryd commented 5 years ago

How would you make sure the vm.$destroy is called on component dismount when using Hook?

antonyrey commented 5 years ago

to implement mount and unmount actions, call "useEffect" on the top level of your function-based component :

useEffect(() => {

// Here is the code called on Mount

return () => {

   // here is the code called on unmount

};

});

dsuryd commented 5 years ago

Give me a proposal on how would you transform the Real-Time HelloWorld example into a Hook function; thanks!

dsuryd commented 5 years ago

Looks like somebody's already done this: https://github.com/lilasquared/use-dotnetify

sdpollack commented 5 years ago

It would be nice to update the documentation to to use hook examples. My team has completely moved away from components to use hooks.

lilasquared commented 5 years ago

@dsuryd I have created a pull request to finish up hooks support for my hook package use-dotnetify https://github.com/lilasquared/use-dotnetify/pull/3.

One thing preventing me from using typescript in the project is this type definition https://github.com/dsuryd/dotNetify/blob/84d9a5e20ba9729de99f75f28a9cde98e185c245/DevApp/src/typings/dotnetify.d.ts#L47

Is it possible for you to create your own type interface that abstracts using the explicit React.Component type?

Edit: I'm actually able to work around this by stubbing out the missing props and casting to React.Component, but since it seems you only ever use state and setState (i see props being used to create a component object but not used within the dotnetifyVM) it might make sense to abstract this anyway.

dsuryd commented 5 years ago

Would it be better if you use this one that doesn't require React.Component: https://github.com/dsuryd/dotNetify/blob/84d9a5e20ba9729de99f75f28a9cde98e185c245/DevApp/src/dotnetify/core/dotnetify.js#L59

lilasquared commented 5 years ago

that definitely would be easier, though for some reason i am getting

Property 'connect' does not exist on type 'typeof dotnetify'.

edit: if i add a d.ts file and reference "dotnetify/dist/dotnetify" i am able to get it to recognize it edit2: nvm that doesn't actually work

dsuryd commented 5 years ago

Hmm, the typing is there: https://github.com/dsuryd/dotNetify/blob/84d9a5e20ba9729de99f75f28a9cde98e185c245/DevApp/src/typings/dotnetify.d.ts#L73 Is it because of the static?

lilasquared commented 5 years ago

Could it be because the typings module default export is named "dotnetify" and the default export from index.js is "dotnetify". It seems i'm not able to access the class only the instance.

lilasquared commented 5 years ago

@antonyrey v2 of use-dotnetify hook is published, if that works for you this issue can probably be closed.

dsuryd commented 5 years ago

Included in v3.5 a simple implementation of React hook useConnect. Documentation is updated with examples.

lilasquared commented 5 years ago

@dsuryd were you able to get around the re-connection issue?

dsuryd commented 5 years ago

I haven't, but will look into it next.