加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

导航 – 如何在NavigatoriOS组件中更新初始路径的道具?

发布时间:2020-12-15 20:12:02 所属栏目:百科 来源:网络整理
导读:这个问题是关于围绕几个组件传递数据对象的策略. 在下面的屏幕截图中,我有一个NavigatorIOS,其中包含ListComponent的初始路由.它在Side-Menu中显示Filters组件.目前,侧面菜单已打开,过滤器可见. 我的目标是当用户更改过滤器中的内容时,我想更新ListComponent
这个问题是关于围绕几个组件传递数据对象的策略.

在下面的屏幕截图中,我有一个NavigatorIOS,其中包含ListComponent的初始路由.它在Side-Menu中显示Filters组件.目前,侧面菜单已打开,过滤器可见.

我的目标是当用户更改过滤器中的内容时,我想更新ListComponent.

我可以使用单例对象来存储过滤器,但我仍然需要找到一种方法来告诉ListComponent它们已经改变了.

var defaultFilters = {
invited: true,joined: false,public: true,private: false,}

class MainTab extends Component {

constructor( props ){
    super(props);

    this.props.filters = defaultFilters;
}

render () {

    var onFilterChange = function ( filters ) {
        console.log("filters changed");
    };

    var filtersComponent = ( <Filters filters={this.props.filters} onFilterChange={onFilterChange.bind(this)} /> );

    return (
        <SideMenu menu = { filtersComponent } touchToClose={true} openMenuOffset={300} animation='spring'>
            <NavigatorIOS 
                style={styles.container}
                initialRoute={{
                    title: 'List',component: ListComponent
                }}

                />
        </SideMenu>
    );
}
}

解决了

var onFilterChange = function ( filters ) {
        console.log("filters changed");

        this.refs.navigator.replace({
            title: 'List',component: ListComponent,passProps: {
              filters: filters
            }
          });
    };

   <NavigatorIOS 
            ref='navigator'
            ...
            />

解决方法

该路由有一个passProps选项,它接受一个props对象,这就是你如何将props传递给导航器中的组件.您可以在onFilterChange中更改MainTab状态,并通过passProps传递该状态.或者,您可能希望查看导航器上的replace方法.

https://facebook.github.io/react-native/docs/navigatorios.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读