React-Native在const中获取状态

时间:2022-03-17 18:57:51

I would like to take state.photo in const but return to me ; connat read property 'photo' of undefined.

我想在const中拍摄state.photo然后回到我身边; connat读取属性'photo'的undefined。

How can i fix this problem ?

我该如何解决这个问题?

class App extends Component{
        constructor(props){
            super(props); 
            this.state={photo:"url"} 

        }

    const RootStack = createStackNavigator(
        {       
          Login:{screen: Login },
          EditProfile:{screen:EditProfileScreen},
          Main:{screen: MainScreen , navigationOptions: {
           headerTitle: (
            <View style={{flexDirection:'row', width:100+"%"}}>
               <AutoHeightImage width={36}  source={  this.state.photo != null ? {uri:url.getURL()+"/"+this.state.photo } : require('./img/user.png')} />
                </TouchableOpacity>
            </View>
            ),
            headerLeft: null,
            headerTintColor: "white",
            headerStyle: {
              backgroundColor: '#34495e'
            } }},

        }, { initialRouteName:"Login "});

1 个解决方案

#1


0  

Unfortunately you won't be able to define a StackNavigator inside a component.

遗憾的是,您将无法在组件内定义StackNavigator。

What you might want to do is override the navigationOptions within the component, as shown here.(You can also read more about the code here. React Navigation docs) And then pass in your photo variable as a prop to the component. Or better yet, as a navigationParam when routing to the desired screen. Check here on how to implement navigationParam.

您可能想要做的是覆盖组件中的navigationOptions,如下所示。(您还可以在此处阅读有关代码的更多信息.React Navigation docs)然后将您的photo变量作为prop传递给组件。或者更好的是,当路由到所需的屏幕时作为navigationParam。在这里查看如何实现navigationParam。

#1


0  

Unfortunately you won't be able to define a StackNavigator inside a component.

遗憾的是,您将无法在组件内定义StackNavigator。

What you might want to do is override the navigationOptions within the component, as shown here.(You can also read more about the code here. React Navigation docs) And then pass in your photo variable as a prop to the component. Or better yet, as a navigationParam when routing to the desired screen. Check here on how to implement navigationParam.

您可能想要做的是覆盖组件中的navigationOptions,如下所示。(您还可以在此处阅读有关代码的更多信息.React Navigation docs)然后将您的photo变量作为prop传递给组件。或者更好的是,当路由到所需的屏幕时作为navigationParam。在这里查看如何实现navigationParam。