【问题标题】:Unable to call or update state of another component React Native [closed]无法调用或更新另一个组件 React Native 的状态 [关闭]
【发布时间】:2018-01-17 02:15:52
【问题描述】:

当我单击子组件中的按钮时,我无法在父组件上触发方法。

这是我的代码:

 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>
    );

我希望当单击子 cmp 上的按钮时,我正在调用子 cmp 中的一个函数,该函数在该调用完成后执行一些后端调用,我希望更新父状态。我尝试了不同的方法,但我无法成功!

【问题讨论】:

  • 如果您向我们展示您尝试过的具体代码以及结果如何,您更有可能获得帮助。询问“我该怎么做 X”的问题被认为是 StackOverflow 的题外话。

标签: javascript reactjs react-native react-router react-redux


【解决方案1】:

如果您需要更新父状态,则需要在父中创建一个更新其状态的函数并将该函数作为道具传递给子。然后你可以通过调用 this.props.whateveryourmethodis 来更新父状态

【讨论】:

猜你喜欢
  • 2019-07-17
  • 2022-01-22
  • 2020-09-12
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 2021-11-30
相关资源
最近更新 更多