React native 组件的构造函数第二次不工作
我有一个组件类:React native 组件的构造函数第二次不工作,react-native,react-navigation,react-navigation-drawer,React Native,React Navigation,React Navigation Drawer,我有一个组件类: class AutoPage extends Component { constructor(props) { super(props); console.log("Auto is " + this.props.route.params.auto); this.state = { avto: this.props.route.params.auto, } this.array = [ this.state.
class AutoPage extends Component {
constructor(props) {
super(props);
console.log("Auto is " + this.props.route.params.auto);
this.state = {
avto: this.props.route.params.auto,
}
this.array = [
this.state.avto.PICTURE,
];
for (let dopImage of this.state.avto.DOP_FOTO.VALUE) {
this.array.push(dopImage);
//console.log(avto);
}
}
}
我正在从另一个地方打开此组件:
<TouchableOpacity onPress={() => navigate('AutoPage', {
auto: item
})}>
导航('AutoPage'{
自动:项目
})}>
这是我的抽屉导航:
<Drawer.Navigator drawerContent={props => <CustomSidebarMenu {...props} />} width={Dimensions.get('window').width - 130}
component={CustomSidebarMenu}>
<Stack.Screen name="AutoListPage" component={AvtoListPageNav} />
<Stack.Screen name="AutoPage" component={AutoPage} options={{ headerTitle: "GorodAvtoPrime", headerTitleAlign: "center" }} />
</Drawer.Navigator>
}width={Dimensions.get('window')。width-130}
组件={CustomSidebarMenu}>
当我单击
touchablepacity
AutoPage
时,会打开它的构造函数。但当返回并再次单击TouchableOpacity时,AutoPage将打开,其构造函数不起作用。我正在this.props.route.params.auto
中获取以前的数据。每次单击TouchableOpacity
时,如何使运行AutoPage的构造函数?您可能需要订阅focus
导航事件。屏幕正在重新使用,因此不会再次创建
你能给我一个简单的例子说明我应该在上面的代码中修复什么吗?我添加了一个例子我是react native的新手,请帮助我。我该从哪里弄到这些方法呢?如何在componentDidMount方法中获取avto属性?
class AutoPage extends Component {
constructor(props) {
super(props);
this.state = {
avto: null,
}
}
componentDidMount() {
this._unsubscribe = this.props.navigation.addListener('focus', () => {
// Update your state here
});
}
componentWillUnmount() {
this._unsubscribe();
}
}