0

説明: DRF でトークン認証に登録した後、トークンと json ユーザー オブジェクトを authReducer に受け取ります。これはさらに rootReducer に渡さauth.userれ、状態として保存されます。userオブジェクトは次のとおりです。

{id:1, first:"first", last:"last", username:"Santa",email:"xyz@gmail.com"}、およびで呼び出すことができます

render(){
console.log(this.props.user);
return(){...

React クラスベースのコンポーネントの render 関数内。

状態は小道具にマッピングされます:

        mapStateToProps=(state)=>{
        return{
        user:state.auth.user,
        }
    }
export default connect(mapStateToProps,)(ProfileUpdate)

すべてが機能します-オブジェクトは上記のようにコンソール内に表示されますが、次のようにオブジェクトの値にアクセスしようとすると:

render(){
const user=this.props.user;
console.log(user.first);
return(){...

私はTypeError: Cannot convert undefined or null to object.

しかし、やっている:

render(){
const user=this.props.user;
user=new Object(user);
console.log(user.first);
return(){...

動作します。何故ですか?常に新しいオブジェクトを作成する必要がありますか?

4

0 に答える 0