简体   繁体   English

如何使用道具中的数据调用 setState? 反应本机

[英]How to call a setState with data from props? React Native

I have a component Home that is called after user get in. There are some data in that screen, and in the header I have a icon button that send the user for a screen where the user can see their profile data and also delete account.我有一个在用户进入后调用的组件 Home。该屏幕中有一些数据,在标题中我有一个图标按钮,用于向用户发送一个屏幕,用户可以在其中查看他们的个人资料数据并删除帐户。 So when the icon button is clicked I'm sending data using props.navigation , sending the user to another screen/component.因此,当单击图标按钮时,我使用props.navigation发送数据,将用户发送到另一个屏幕/组件。

profile = () => {
        const { navigation } = this.props;
        const user = navigation.getParam('user', 'erro');
        this.props.navigation.navigate('profileScreen', { user: user });
    }

Inside the new component, I tried to setState inside the method componentDidMount using that data but it didn't work.在新组件中,我尝试使用该数据在方法componentDidMount中设置状态,但没有奏效。 I checked using console.log the data is there.我使用console.log检查数据在那里。 How could I setState in this case?在这种情况下我怎么能 setState ?

export default class profileScreen extends Component {
    static navigationOptions = {
        title: "Profile"
    };

constructor(props) {
    super(props);
    this.state = {
        user: {}
    }
}

componentDidMount() {
    const {navigation} = this.props;
    const user = navigation.getParam('user', 'Erro2');
    this.setState({user: user.user});
    console.log(this.state); // Object {"user": Object {},}
    console.log("const user");
    console.log(user.user); //my actual data is printed
}

render() {
    return (
        <Text>{this.state.use.name}</Text>
        <Text>{this.state.use.age}</Text>
        <Text>{this.state.use.email}</Text>
        ...
        ...
    )
}

} }

Result from console.log(this.state)结果来自 console.log(this.state)

Object {
  "user": Object {},
}

Result from console.log(user)结果来自 console.log(user)

Object {
  "createdAt": "2019-04-27T21:21:36.000Z",
  "email": "sd@sd",
  "type": "Admin",
  "updatedAt": "2019-04-27T21:21:36.000Z",
  ...
  ...
}

It seems like you're trying to send an object ( user ) as a route parameter with react-navigation library.似乎您正在尝试使用react-navigation库将对象( user )作为路由参数发送。 It's not possible.这是不可能的。

The proper way of doing such scenarios is to send the user's id userId as route parameter and load user details from your API (or state).执行此类场景的正确方法是将用户的 id userId作为路由参数发送并从您的 API(或状态)加载用户详细信息。

profile = () => {
        const user = {id: 10 /*, ... rest of properties */}; // or take it from your state / api
        this.props.navigation.navigate('profileScreen', { userId: user.id });
    }



componentDidMount() {
    const {navigation} = this.props;
    const userId = navigation.getParam('userId', 'Erro2');
    // const user = read user details from state / api by providing her id
    this.setState({user: user});
}

ps: if you are using state management like redux/flux/..., consider setting currentUser in your global state and read that instead of passing userId as a route param. ps:如果你正在使用像 redux/flux/... 这样的状态管理,考虑在你的全局状态中设置currentUser并读取它而不是将 userId 作为路由参数传递。

To make sure component updates when the user got new value in the state render method should be like this:为了确保当用户在 state 渲染方法中获得新值时组件更新应该是这样的:

render() {
    const {user} = this.state
    return (
      <View>
        {user && <Text>{user.name}</Text>}
        {user && <Text>{user.age}</Text>}
        {user && <Text>{user.email}</Text>}
        ...
        ...
     </View>
    )
}

Note 0: const {user} = this.state would save you from repeating this.state注意 0: const {user} = this.state重复this.state

Note 1: it would much more elegant to wrap all those <Text> component inside another <View> to prevent repeating the condition phrase {user && ...}注意 1:将所有这些<Text>组件包装在另一个<View>以防止重复条件短语{user && ...}会更优雅

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

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