Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么在渲染内返回之前向变量添加状态值_Javascript_Reactjs_State - Fatal编程技术网

Javascript 为什么在渲染内返回之前向变量添加状态值

Javascript 为什么在渲染内返回之前向变量添加状态值,javascript,reactjs,state,Javascript,Reactjs,State,我经常在JavaScript类中看到下面的模式并作出反应 class Welcome extends React.Component { state: { someValue: 1 } render() { const myValue = this.state.someValue; return <h1>Hello, {myValue}</h1>; } } 类欢迎扩展React.Component{ 声明:{ someValue

我经常在JavaScript类中看到下面的模式并作出反应

class Welcome extends React.Component {
  state: {
   someValue: 1
  }

  render() {
    const myValue = this.state.someValue;

    return <h1>Hello, {myValue}</h1>;
  }
}
类欢迎扩展React.Component{
声明:{
someValue:1
}
render(){
const myValue=this.state.someValue;
返回Hello,{myValue};
}
}

我的问题是,,在返回之前声明变量状态的意义/好处是什么?当您可以在返回中使用
this.state.someValue
访问值时?

赋值或解构的好处是,当您多次使用同一变量时,您可以从数组中选择多个变量,而在这种情况下,您不需要需要一次又一次地写
这个状态


在上述情况下,您可以简单地使用state中的值,在使用之前赋值没有任何好处。

它主要用于解构。