一般来说,我对编程相当陌生,对JS和React(本机)甚至更新,但我已经为此工作了一整天,但我仍然没有弄明白,所以我求助于堆栈溢出,希望有人能帮助我.
基本上,我想完成的是将其他Components
个设置为应用程序
组件的子组件,因为我希望他们能够访问我将在应用程序
的state
中设置的信息.然而,同时,我也在使用react-navigation
来创建底部导航栏,因此我不知道如何将应用程序
中的props
传递给其他Components
个,比如代表其他children components
的ExplorePage
组件.
应用程序
import React from 'react';
import ExplorePage from './app/tabs/ExplorePage';
import {createBottomTabNavigator} from 'react-navigation';
...other imports
class 应用程序 extends React.Component {
state = {
parentState: 'testing testing',
}
}
const MainScreenNavigator = createBottomTabNavigator(
{
Home: {screen: ExplorePage},
Search: {screen: SearchPage},
Favorites: {screen: FavoritesPage},
}
);
export default MainScreenNavigator;
ExplorePage,与SearchPage和FavoritesPage类似
...imports
export default class ExplorePage extends React.Component {
constructor(props) {
super(props);
this.state = {
}
}
componentDidMount() {
console.log(this.props.parentState ? this.props.parentState : "Parent state does not exist what do :(");
}
render(){
return(
<Text>Testing</Text>
)
}
And obviously every time the console prints that parentState
does not exist. I thought that being in the same place would give the other components
like ExplorePage
props of 应用程序
. Thanks for helping me!