当屏面放置在<Stack.Navigator>
内时,状态未更新
export default function App() {
const Stack=createNativeStackNavigator()
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name='TestScreen' children={TestScreen}/>
</Stack.Navigator>
</NavigationContainer>
);
}
测试屏幕如下:
const TestScreen = () => {
const [currentCell, setCurrentCell] = useState(1)
return (
<SafeAreaView>
<Text>{currentCell}</Text>
<Button
title='change'
onPress={()=>setCurrentCell(currentCell==1?2:1)}
/>
</SafeAreaView>
);
}
如果应用程序被替换为以下代码,则它可以工作
export default function App() {
const Stack=createNativeStackNavigator()
return (
<NavigationContainer>
<TestScreen/>
</NavigationContainer>
);
}
1条答案
按热度按时间bzzcjhmw1#
正如@YoussoufOumar在评论中提到的,
Stack.Screen
需要一个component
,而不是children
。这里有一个小吃来测试它https://snack.expo.dev/@mohammed-handa/state-not-updating-inside-stack-navigator