useStoreState doesn't react to changes in the state after Fast Refresh in React Native
See original GitHub issueHi,
I added easy-peasy(v.3.3.0) successfully into a new React Native(v0.62.2) project but noticed that a function component using useStoreState will not react to the state after editing the component code. My button will send a request, receive a new picture of a dog and set that dog into the state but my component will not detect the state change. I have React Native’s Fast Reload enabled.
Here is my test component (TestComponent.tsx):
import ...
export function TestComponent() {
const dog = useStoreState((state) => state.test.dog);
const getDog = useStoreActions((actions) => actions.test.getDog);
const handleGetDogPress = useCallback(async () => {
try {
await getDog();
} catch {
console.log('Simulated api error occurred');
}
}, [getDog]);
return (
<View style={styles.container}>
{dog && <Image style={styles.logo} source={{uri: dog}} />}
<Button title="Get Dog" onPress={handleGetDogPress} />
</View>
);
}
const styles = StyleSheet.create(...)
Fast Refresh documentation points out that it’ll try to preserve the state if a change is done to a file that only exports a React component:
When possible, Fast Refresh attempts to preserve the state of your component between edits. In particular, useState and useRef preserve their previous values as long as you don’t change their arguments or the order of the Hook calls.
If I change the order of the useStoreState and useStoreActions, the useStoreState hook will work until I make a modification that makes Fast Refresh try to preserve values of useStates and useRefs. Putting // @refresh reset
somewhere in the file also works but it forces the component to re-render on every save. Disabling Fast Refresh works as well but it makes the developer experience really bad.
Maybe the bug is related to the useRef hooks used in the useStoreState? I checked the code but I’m afraid I don’t know enough about hooks/redux/easy-peasy yet to fix it myself.
Is there anyone that can look into this?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:12 (3 by maintainers)
Hi all, I want to look into this. Are you all running the Expo CLI, and RN Web?
Can also confirm this is happening to me as well. Anything I can do that would help with getting this looked into? 😃