【问题标题】:Why my navigator is not working?为什么我的导航器不工作?
【发布时间】:2017-07-12 17:30:13
【问题描述】:

我有这个结构,我有一个MyComponent 的列表:

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() 没有转到另一个屏幕。

我还尝试在MyScreen 类中准备导航器,然后将其作为函数传递给MyComponent。但也不行:

class MyComponent extends Component 
{
    props: { OnPress: Function, data: Object };

    render() 
    { 
        return (
                <Menu>
                    <MenuTrigger>  <Text> Menu </Text>  </MenuTrigger>
                    <MenuOptions>
                        <MenuOption onSelect={() => this.OnPress.bind(this)}  text='Show Screen B' />
                    </MenuOptions>
                </Menu>
        );
    }
}

class MyScreen extends Component 
{   
    ShowScreenB(data: Object){
        this.props.navigation.navigate('ScreenB', {data});    
    }

    render()     
    {
        let renderRow = (row) => { return (<MyComponent data= {row} OnPress= {this.ShowScreenB.bind(this, row)} /> );}

        return (
            <View >
                <ListView dataSource={this.state.DataSource} renderRow={renderRow.bind(this)}/>
            </View>
        );
    }
}

可能是什么问题?

编辑:MenuPopUp Menu

【问题讨论】:

    标签: javascript reactjs react-native react-native-ios react-navigation


    【解决方案1】:

    你永远不会打电话给ShowScreenB()

    现在你只是绑定它:

    onSelect={() => this.ShowScreenB.bind(this, this.props.data)}
    

    bind 不调用该函数。它所做的一切都是将它绑定到给定的上下文。您需要实际调用ShowScreenB() 以便您的导航代码执行。例如:

    onSelect={() => { this.ShowScreenB.bind(this); this.ShowScreenB(this.props.data); }}
    

    编辑以回复评论,因为它不适合评论:

    那是因为删除 ( ) =&gt; 后,您所拥有的只是 { } 语法的剩余部分。 { } 表示评估括号内的内容。看看我的答案中链接中写的内容。 bind的返回值为:

    具有指定 this 值和初始参数的给定函数的副本。

    所以表达式{ this.ShowScreenB.bind(this) } 将计算返回值;因此调用导航功能。我在上面发布的内容只是您可以做的一个示例。你也可以把它写成onSelect={() =&gt; this.ShowScreenB.bind(this)()},它也可以。如果您对此有疑问,您还应该了解arrow functions 的工作原理。

    【讨论】:

    • 谢谢,如果我像这样从MenuOption 中删除() =&gt;,它会起作用:&lt;MenuOption onSelect= {this.OnPress.bind(this)} text='Show Screen B' /&gt;
    • @KyleKhalaf 编辑回答您的评论。
    • 感谢箭头函数链接!我同意我需要它:)
    猜你喜欢
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 2015-10-11
    相关资源
    最近更新 更多