【发布时间】:2019-10-12 19:14:24
【问题描述】:
我已将自定义 component 导入我的 screen 并在 render() 函数中呈现它。然后,为该自定义组件创建了一个ref。现在,render() 函数看起来就像这样。
render() {
return (
<View>
<MyComponent ref={component => this.myComponent = component} />
</View>
)
}
然后,我创建了另一个函数来访问我的自定义组件的状态。我是这样写的。
myFunction = (ref) => {
ref.setState({ myState: myValue })
}
然后,我这样调用那个函数。
this.myFunction(this.myComponent)
但是,它不起作用。它给了我以下错误。
null is not an object (evaluating 'ref.setState')
其实我需要这个myFunction做的是,
this.myComponent.setState({ myState: myValue })
你能帮我解决这个问题吗?
【问题讨论】:
标签: javascript react-native ref