简体   繁体   English

虽然this.state是一个Javascript对象,但即使将状态传递给有数据的状态,为什么状态也为null?

[英]while this.state is a Javascript object , why is state null even after passing into it a state that has data?

I have the following code (see below) 我有以下代码(见下文)

I want it to display the username which is a combination of firstName and lastName . 我希望它显示由firstName和lastName组合而成的用户名。 Why doesn't the below code work ? 为什么下面的代码不起作用?

I expect it to display 我希望它能显示

Welcome Daggie Blanqx ! 欢迎Daggie Blanqx!

class Header extends React.Component{

    constructor(props){
        super(props);
        this.state = {
            firstName : 'Daggie',
            lastName :  'Blanqx',
            userName : ()=>(this.state.firstName + this.state.lastName)
        }


    }

    render(){
        return(
            <div>
                <p> Welcome {this.state.userName} !</p>
            </div>
            )
    }
}

You are missing with () 您缺少()

<p> Welcome {this.state.userName()} !</p>

Also no need to define a function for this directly get the result like this 也不需要为此定义函数直接获得这样的结果

<p>Welcome , {this.state.firstName} {this.state.lastName} !</p>

It seems like you are inserting a function to your state and hence you need to call it to get the result. 似乎您正在向状态插入一个函数,因此您需要调用它来获取结果。 Generally putting functions like this in the state is not recommended. 通常不建议将这样的功能置于状态。 However, if you still want to go with that approach, try the following: 但是,如果您仍然想使用该方法,请尝试以下操作:

<p>Welcome , {this.state.userName()} !</p>

An even better approach would be to remove the function from the state and do like this: 更好的方法是从状态中删除该函数并执行以下操作:

getUserName() {
   return this.state.firstName + this.state.lastName
}

And then in your template: 然后在您的模板中:

<p>Welcome , {this.getUserName()} !</p>

in your example you are accessing the the function this.state.userName you need to call that like this this.state.userName() 在您的示例中,您正在访问函数this.state.userName您需要像this.state.userName()这样调用它
this.state.userName will return the function variable you need to put () at end of your function get return value of the function this.state.userName将返回您需要在函数末尾添加()function变量,以获取该函数的return
You you understood why its not working 您了解为什么它不起作用
Try to avoid using methods in your state object 尝试避免在状态对象中使用方法

It should throw an error because you're trying to interact with the state but state hasn't been initialized yet. 它应该引发错误,因为您正在尝试与状态进行交互,但状态尚未初始化。 For that purposes, use componentDidMount() + prevState or just put it inside render() : 为此,请使用componentDidMount() + prevState或将其放入render()

Variant 1 : 变体1

import React, { Component } from 'react'

export default class Header extends Component {

    constructor(props) {
        super(props)
        this.state = {
            firstName : 'Daggie',
            lastName :  'Blanqx'
        }


    }

    render() {
        return(
            <div>
                <p> Welcome {this.state.firstName + this.state.lastName} !</p>
            </div>
            )
    }
}

Variant 2 : 变体2

import React, { Component } from 'react'

export default class Header extends Component {

    constructor(props) {
        super(props)
        this.state = {
            firstName : 'Daggie',
            lastName :  'Blanqx',
            fullName: ''
        }
    }
    componentDidMount() {
      this.setState(prevState => ({
        fullName: prevState.firstName + prevState.lastName
      }))
    }

    render() {
        return(
            <div>
                <p> Welcome {this.state.fullName} !</p>
            </div>
            )
    }
}

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

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