question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useStoreState doesn't react to changes in the state after Fast Refresh in React Native

See original GitHub issue

Hi,

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:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ctrlplusbcommented, Oct 8, 2020

Hi all, I want to look into this. Are you all running the Expo CLI, and RN Web?

1reaction
DaneEverittcommented, Jul 24, 2020

Can also confirm this is happening to me as well. Anything I can do that would help with getting this looked into? 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why React doesn't update state immediately
When developing React applications, you may have noticed that state updates don't immediately reflect new values after being changed.
Read more >
React component not updating when store state has changed
The state is 100% changing, however the component doesn't refresh. import React, { Component } from 'react' import { connect } from ...
Read more >
Fast Refresh
Fast Refresh. Fast Refresh is a React Native feature that allows you to get near-instant feedback for changes in your React components.
Read more >
Easy-Peasy State Management Using React ...
Learn how easy it is to manage your app's state with this React library.
Read more >
easy-peasy
3.1 to v5.0.3 and my async store doesnt work anymore. I have persistor import AsyncStorage from '@react-native-async-storage/async- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found