Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

use state - useState in "React Native" does not display initial value when I change my code

I have a problem with useState hook in my React Native app. My app doesn't refresh the state to its initial value when I modify my code. I have this problem in both "iOS Simulator" and "Android Emulator". However, the state works fine when I switch to class component.

Here is a small example to illustrate this problem:

export default function App() {

  const [name, setName] = useState("Mark");

  return (
    <View>
      <Text>{ name }</Text>
      <Button title="Change name" onPress={() => setName("Eric")} />
    </View>
  );
}

When I click the button I change "Mark" to "Eric". But when I modify the code (lets say, I add a simple <Text>Hello</Text> component) and save it I still see the latest value in the state "Eric".

Normally the component is supposed to reload when I make changes to the code and therefore display the initial value "Mark". Am I right? So what's wrong here? Is there any recent changes to "useState" behavior?

By the way, I use the following versions in my projet:

  • expo-cli 4.1.6 (Same problem with version 4.0.17 too)
  • watchman 4.9.0
  • node 14.15.4 LTS

Dependencies:

Dev Dependencies:

  • @babel/core: ~7.9.0
question from:https://stackoverflow.com/questions/66060844/usestate-in-react-native-does-not-display-initial-value-when-i-change-my-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...