react-hooks icon indicating copy to clipboard operation
react-hooks copied to clipboard

useChannel Borked

Open perkins2099 opened this issue 3 years ago • 1 comments

useChannel doesnt have access to any state.

The data gets refreshed from the server.

When I try to check the data value from the server its undefined. So If i wanted to update state from the ably message I cannot.

const [data, setData] = useState();

configureAbly({ key: ablyClientKey, clientId: "1232" });

const [channel, ably] = useChannel(`channel`, (message) => {
    console.log(data) // This is triggered minutes later from the ruby code as data is updated and is always the initialized value not the refreshed value.
  });

  useEffect(() => {
    refreshData()
  }, []);

  function refreshData() {
    axios.get(`/get_data`, {  })
      .then(res => {
        setData(res)
      }).catch((error) => {
      // Error
        console.log(error.response);
      })
  }

perkins2099 avatar Oct 07 '22 15:10 perkins2099

The useChannel callback is defined once and won't be redefined on the component rerender. This is not an expected behavior, not in a react component anyway.

Also, having a skip parameter like Apollo that subscribes to a channel conditionnally would be nice @thisisjofrank

younes0 avatar Oct 24 '22 21:10 younes0

@younes0 I agree that a skip parameter would be SO helpful.

cor1 avatar Dec 15 '22 21:12 cor1

This is what I ended up doing as a workaround:

const [channel, ably] = useChannel("channel", (message) => {
    setData(prevState => console.log(prevState)) // You can access the current state using prevState parameter of the hook
});

lkenny97 avatar Jun 04 '23 12:06 lkenny97

➤ Automation for Jira commented:

The link to the corresponding Jira issue is https://ably.atlassian.net/browse/SDK-3755

sync-by-unito[bot] avatar Jul 24 '23 16:07 sync-by-unito[bot]