ios - React Native Reload Screen A In Back 动作
问题描述
我有 ScreenA 要单击 Next ScreenB 然后回到屏幕 A 不调用函数 componentWillMount()
ScreenA -> Next -> ScreenB -> Back() -> ScreenA
如何在返回操作中重新加载路由屏幕
类屏幕A
import React from "react";
import { Button, Text, View } from "react-native";
class ScreenA extends Component {
constructor(props){
super(props)
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
})
}
}
componentWillMount() {
fetch(MYCLASS.DEMAND_LIST_URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
userId:'17'})
})
.then((response) => response.json())
.then((responseData) => {
if (responseData.status == '1') {
var data = responseData.data
this.setState({
dataSource: this.state.dataSource.cloneWithRows(data),
});
}
})
.done();
}
onPress = () => {
this.props.navigate("ViewB");
};
render() {
return (
<View>
<Text>test</Text>
<Button title="Next" onPress={this.onPress} />
</View>
);
}
}
类屏幕B
从“react”导入 React 从“react-native”导入 { Button }
class ScreenB extends Component {
render() {
const {goBack} = this.props.navigation;
return(
<Button title="back" onPress={goBack()} />
)
}
}
解决方案
类屏幕A
import React from "react";
import { Button, Text, View } from "react-native";
class ScreenA extends Component {
constructor(props){
super(props)
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
})
}
}
componentWillMount() {
this.getData()
}
getData() {
fetch(MYCLASS.DEMAND_LIST_URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
userId:'17'})
})
.then((response) => response.json())
.then((responseData) => {
if (responseData.status == '1') {
var data = responseData.data
this.setState({
dataSource: this.state.dataSource.cloneWithRows(data),
});
}
})
.done();
}
onPress = () => {
this.props.navigate("ViewB", { onSelect: this.onSelect, getData: () => this.getData() });
};
render() {
return (
<View>
<Text>test</Text>
<Button title="Next" onPress={this.onPress} />
</View>
);
}
}
类屏幕B
class ScreenB extends Component {
componentWillUnmount() {
this.props.navigation.state.params.getData()
}
render() {
const {goBack} = this.props.navigation;
return(
<Button title="back" onPress={goBack()} />
)
}
}
推荐阅读
- google-app-engine - 如何使部署到谷歌云应用引擎的应用程序可用于某些白名单 IP
- java - 将类的实例添加到 Arraylist
- mysql - 用 Where 删除。程序会删除所有内容。为什么?
- java - 如何知道自定义 Java 节点是否包含在第二个节点中?
- android - 在 Android 上监听连接变化
- python - 如何在 Keras 中格式化 ImageDataGenerator 的 X 和 Y 数据?
- python - Can't install gcloud on Amazon Linux : invalid syntax
- swift - 如何替换字符串中出现的有限次数
- sql - 对 ODBC 表使用多个左连接的查询失败
- python - Pyspark 用 NULL 替换 NaN