useChannel Borked
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);
})
}
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 I agree that a skip parameter would be SO helpful.
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
});
➤ Automation for Jira commented:
The link to the corresponding Jira issue is https://ably.atlassian.net/browse/SDK-3755