Javascript 为什么在渲染内返回之前向变量添加状态值
我经常在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
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中的值,在使用之前赋值没有任何好处。它主要用于解构。