如何在NavigatoriOS组件中更新初始路由的道具?

Jos*_*man 5 navigation react-native

这个问题是关于围绕几个组件传递数据对象的策略.

在下面的屏幕截图中,我有一个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>
    );
}
}
Run Code Online (Sandbox Code Playgroud)

解决了

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

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

   <NavigatorIOS 
            ref='navigator'
            ...
            />
Run Code Online (Sandbox Code Playgroud)

Col*_*say 5

该路线有一个passProps接受道具对象的选项,这就是你如何将道具传递到导航器中的组件.您可能会改变您的MainTab状态onFilterChange并将其传递给您passProps.或者,您可能希望查看replace导航器上的方法.

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