Javascript 为什么我的导航器不工作?
我有这个结构,其中有一个MyComponent的列表:Javascript 为什么我的导航器不工作?,javascript,reactjs,react-native,react-native-ios,react-navigation,Javascript,Reactjs,React Native,React Native Ios,React Navigation,我有这个结构,其中有一个MyComponent的列表: class MyComponent extends Component { props: { navigation: Object, data: Object }; ShowScreenB(data: Object){ this.props.navigation.navigate('ScreenB', {data}); } render() { ret
class MyComponent extends Component
{
props: { navigation: Object, data: Object };
ShowScreenB(data: Object){
this.props.navigation.navigate('ScreenB', {data});
}
render()
{
return (
<Menu>
<MenuTrigger> <Text> Menu </Text> </MenuTrigger>
<MenuOptions>
<MenuOption onSelect={() => this.ShowScreenB.bind(this, this.props.data)} text='Show Screen B' />
</MenuOptions>
</Menu>
);
}
}
class MyScreen extends Component
{
render()
{
let renderRow = (row) => { return (<MyComponent data= {row} navigation= {this.props.navigation} /> );}
return (
<View >
<ListView dataSource={this.state.DataSource} renderRow={renderRow.bind(this)}/>
</View>
);
}
}
可能是什么问题
编辑:
菜单是一个。你永远不会调用ShowScreenB()
现在您正在绑定它:
onSelect={() => this.ShowScreenB.bind(this, this.props.data)}
不调用函数。它所做的一切都将它绑定到给定的上下文。您需要实际调用ShowScreenB()
,以便执行导航代码。例如:
onSelect={() => { this.ShowScreenB.bind(this); this.ShowScreenB(this.props.data); }}
编辑以回答评论,因为它不适合评论:
这是因为删除()=>
会使您只剩下剩下{}
语法{}
表示评估括号内的内容。看看我的答案中链接中写的内容。bind
的返回值为:
具有指定此值和初始参数的给定函数的副本
因此表达式{this.ShowScreenB.bind(this)}
将计算返回值;因此调用导航函数。我在上面发布的只是你能做的一个例子。您可以将其编写为onSelect={()=>this.ShowScreenB.bind(this)()}
,它也可以工作。如果您在这方面遇到了问题,您还应该重新学习如何工作。谢谢,如果我从菜单选项中删除()=>
,如下所示:
@KyleKhalaf编辑以回答您的评论。感谢箭头函数链接!我同意我需要它:)
onSelect={() => { this.ShowScreenB.bind(this); this.ShowScreenB(this.props.data); }}