我正在开发React本机Expo App并使用Hooks开发React导航版本5。如何更新goBack()上的状态?
const fetchData = async (props) => {
//http request
}
useEffect(() => {
fetchData(props);
}, [props]);
return(
<View>
<TouchableOpacity onPress={() => props.navigation.navigate('Settings',{onGoBack, () => fetchData(props)})}>
</TouchableOpacity
</View>
)
const submit = () => {
props.navigation.route.params.onGoBack();
props.navigation.goBack();
}
在导航状态下发现无法序列化的值,这可能会破坏诸如持久和恢复状态之类的用法。如果您在参数中传递了不可序列化的值(例如函数,类实例等),则可能会发生这种情况。如果需要在选项中使用带有回调的组件,则可以使用“ navigation.setOptions”。有关更多详细信息,请参见https://reactnavigation.org/docs/troubleshooting#i-get-the-warning-non-serializable-values-were-found-in-the-navigation-state。
答案 0 :(得分:1)
您可以使用final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new DefaultsModule());
Assertions.assertNull(objectMapper.readValue("null", Generated.class));
Assertions.assertEquals(new Generated(null, Collections.emptyList()), objectMapper.readValue("{}", Generated.class));
Assertions.assertEquals(new Generated(null, ImmutableList.of("bar")), objectMapper.readValue("{\"array\":[\"bar\"]}", Generated.class));
Assertions.assertEquals(new Generated("foo", Collections.emptyList()), objectMapper.readValue("{\"string\":\"foo\"}", Generated.class));
Assertions.assertEquals(new Generated("foo", null), objectMapper.readValue("{\"string\":\"foo\",\"array\":null}", Generated.class));
Assertions.assertEquals(new Generated("foo", Collections.emptyList()), objectMapper.readValue("{\"string\":\"foo\",\"array\":[]}", Generated.class));
而不是props.navigation.navigate
将参数传递到上一个屏幕。
看到这个文件