简体   繁体   English

在React Native IOS标签栏中获取状态

[英]Get state in React Native IOS Tab Bar

I'm a bit new to react-native , so I might not have understood this while reading the docs but. 我对react-native有点react-native ,所以我在阅读文档时可能不了解这一点,但是。 I have a tab bar in an ios app with 2 tabs. 我在带有2个标签的ios应用中有一个标签栏。

Currently the view code for one of the tabs is in the same file as the page that loads the the Tab bar (I call this HomeTab ). 当前,其中一个选项卡的视图代码与加载选项卡栏的页面在同一文件中(我称为HomeTab )。 The second tab (call Tab2 ) which contains the view code for the second tab is in a separate .js page and is loaded into HomeTab . 包含第二个选项卡的视图代码的第二个选项卡(称为Tab2 )位于单独的.js页面中,并已加载到HomeTab

I'm having trouble passing information in HomeTab's state to Tab2 , and do not want to write all of Tab2 into HomeTab like I did for Tab1 because I will get a really big file. 我有在HomeTab的状态信息传递麻烦Tab2 ,不希望将所有的写Tab2HomeTab像我一样对Tab1 ,因为我会得到一个非常大的文件。

The code structure is a bit like this: 代码结构有点像这样:

    {View Code for Tab 1 here}

    <TabBarIOS selectedTab={this.state.selectedTab}>
            <TabBarIOS.Item
                selected={this.state.selectedTab === 'Tab1'}
                title = "Tab1"
                icon = {require('image!Tab1')}
                onPress={() => {
                    this.setState({
                        selectedTab: 'Tab1'
                    }

                    );
                }}>

                // Call Tab1 View here

            </TabBarIOS.Item>

            <TabBarIOS.Item
                selected={this.state.selectedTab === 'Tab2'}
                title = "Tab2"
                icon = {require('image!Tab2')}
                onPress={() => {
                    this.setState({
                        selectedTab: 'Tab2'
                    }
                    );
                }}>

                <Tab2/>
            </TabBarIOS.Item>

Intuitively I feel like all I am doing is putting code in a separate file, so both tabs should have the same state, but when I print out anything from my state it is undefined. 凭直觉,我觉得我所做的就是将代码放在单独的文件中,因此两个选项卡应具有相同的状态,但是当我从状态中打印出任何内容时,它是未定义的。 Any advice would be appreciated! 任何意见,将不胜感激! Thanks. 谢谢。

I was fairly ignorant of the capabilities of javascript. 我对JavaScript的功能一无所知。 I was able to pass props just by including it in the call to Tab2 like this 我能够通过这样将道具包含在对Tab2的调用中来传递道具

    <Tab2 propName = {some information}/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM