无法调用或更新另一个组件 React Native 的状态 [英] Unable to call or update state of another component React Native
问题描述
当我单击子组件中的按钮时,我无法在父组件上触发方法.
I am unable to fire a method on the parent component when I click a button from a child component.
这是我的代码:
constructor(props) {
super(props);
this.state = {
text: '',
};
}
nav = DrawerNavigator({
Child: {
screen: Child,
navigationOptions: {
title: 'Child Component'
}
},
Profile: {
screen: Profile
},
}, {
screen: Profile,
contentComponent: DrawerContent
});
const DrawerContent = (props) => (
<View> {this.state.text}</View>
})
const
nav2= DrawerNavigator({
Child: {
screen: Child,
navigationOptions: {
title: 'Child cmp'
}
},
Profile: {
screen: Profile
},
screen: Manager
}
}, {
screen: Profile,
contentComponent: DrawerContent
});
const Layout = createRootNavigator(signedIn);
return (
<View>
<Layout/>
</View>
);
我希望当单击 Child cmp 上的按钮时,我正在调用 Child cmp 中的一个函数,该函数在该调用完成后执行一些后端调用,我希望更新父状态.我尝试了不同的方法,但都没有成功!
I want so when a button on Child cmp is clicked i am calling a function within the Child cmp which does some backend call after that call finishes, I want the Parent state to update. I tried different ways, but I couldnt succeed!
推荐答案
如果您需要更新父状态,则需要在父中创建一个函数来更新其状态并将该函数作为道具传递给子.然后你可以通过调用 this.props.whateveryourmethodis 来更新父状态
If you need to update the parent state, you will need to create a function in the parent that updates its state and pass that function to the child as a prop. Then you can update the parent state by calling this.props.whateveryourmethodis
这篇关于无法调用或更新另一个组件 React Native 的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!