我没有从反应导航推送中收到上下文

时间:2019-12-05 05:52:55

标签: reactjs react-native react-navigation react-hooks

我的React Native App中有一个带有一些组件的stacknavigator:

const AppNavigator = createStackNavigator({Test1,Test2,Test3})

我也有上下文api包装了我的组件:

<UserContext.Provider  value={{test,setTest}}>
   <Test1/>
</UserContext.Provider>

在Test1内部,我具有Test2组件,此处在Test2中显示了上下文。而且在Test1中,我还有一个按钮可以推到Test3。

TEST1:

<Test2/>
<Fab
    onPress={() => navigation.push('Test3')}
    containerStyle={{}}
    style={{backgroundColor: '#5067FF'}}
    position="bottomRight">
    <Icon name="share" />
</Fab>

TEST2:

const Test2 = ({navigation}) => {
  const context = useContext(UserContext);
  console.log(context) //it's ok
}

问题是上下文在Test3中未到达,显示为空

TEST3

const Test3 = ({navigation}) => {
  const context = useContext(UserContext);
  console.log(context) //null
}

这是怎么回事? 如何在Test3中接收上下文?

0 个答案:

没有答案