Javascript 为另一个键的状态对象键赋值';反应js中的s值

Javascript 为另一个键的状态对象键赋值';反应js中的s值,javascript,reactjs,Javascript,Reactjs,我试图检查三个数字之和是否相等。如果是,则我要更新状态 state = { number1 : Math.floor(Math.random() * 100), number2 : Math.floor(Math.random() * 100), number3 : Math.floor(Math.random() * 100), proposedAnswer : Math.floor(Math.random() * 3) + this.number1 + thi

我试图检查三个数字之和是否相等。如果是,则我要更新状态

state = {
    number1 : Math.floor(Math.random() * 100),
    number2 : Math.floor(Math.random() * 100),
    number3 : Math.floor(Math.random() * 100),
    proposedAnswer : Math.floor(Math.random() * 3) + this.number1 + this.number2 + this.number3
}
但是当我尝试使用
{this.state.proposedAnswer}
时,我会得到NAN吗?
有什么帮助吗?

在初始化对象之前,不能引用它的属性。。。也许像这样的事情在你的情况下会起作用

let number1=Math.floor(Math.random()*100);
设number2=Math.floor(Math.random()*100);
设number3=Math.floor(Math.random()*100);
此.state={
第一,
第二,
第三,
建议答案:Math.floor(Math.random()*3)+number1+number2+number3
}

console.log(this.state)在初始化对象之前,不能引用该对象的属性。。。也许像这样的事情在你的情况下会起作用

let number1=Math.floor(Math.random()*100);
设number2=Math.floor(Math.random()*100);
设number3=Math.floor(Math.random()*100);
此.state={
第一,
第二,
第三,
建议答案:Math.floor(Math.random()*3)+number1+number2+number3
}

console.log(this.state)错误在于您没有按状态访问这些变量。相反,您应该这样做:

state = {
    number1 : Math.floor(Math.random() * 100),
    number2 : Math.floor(Math.random() * 100),
    number3 : Math.floor(Math.random() * 100),
    proposedAnswer : ''
}
由于状态已初始化,现在我们可以定义一个函数来计算和

sum = () => {
  this.setState ({
    proposedAnswer:  Math.floor(Math.random() * 3) + this.state.number1 + this.state.number2 + this.state.number3
  )}
}

你做错了的是你没有按状态访问这些变量。相反,您应该这样做:

state = {
    number1 : Math.floor(Math.random() * 100),
    number2 : Math.floor(Math.random() * 100),
    number3 : Math.floor(Math.random() * 100),
    proposedAnswer : ''
}
由于状态已初始化,现在我们可以定义一个函数来计算和

sum = () => {
  this.setState ({
    proposedAnswer:  Math.floor(Math.random() * 3) + this.state.number1 + this.state.number2 + this.state.number3
  )}
}

代码中的
this
没有引用任何内容,因此将
undefined
相加为一个数字,然后得到
NaN

您可以使用setState的回调版本,在该版本中传递一个函数,该函数接受一个状态并返回一个新状态。 在PureComponent中,您可以编写此函数,以便在某个测试为真时返回旧状态

this.setState((state) => {
  const number1 = Math.floor(Math.random() * 100);
  const number2 = Math.floor(Math.random() * 100);
  const number3 = Math.floor(Math.random() * 100);
  const proposedAnswer : Math.floor(Math.random() * 3) + number1 + number2 + number3

  if (proposedAnswer ...) {
    // return a new state
  }
  // in the default case you return the old state unchanged
  return state;
})

请注意,我不建议使用此模式,因为您的更新函数不是纯函数。如果您可以为您当前的用例添加任何细节,也许我可以详细说明需要哪种模式。

代码中的
没有引用任何内容,因此您将
未定义的
求和为一个数,然后得到
NaN

您可以使用setState的回调版本,在该版本中传递一个函数,该函数接受一个状态并返回一个新状态。 在PureComponent中,您可以编写此函数,以便在某个测试为真时返回旧状态

this.setState((state) => {
  const number1 = Math.floor(Math.random() * 100);
  const number2 = Math.floor(Math.random() * 100);
  const number3 = Math.floor(Math.random() * 100);
  const proposedAnswer : Math.floor(Math.random() * 3) + number1 + number2 + number3

  if (proposedAnswer ...) {
    // return a new state
  }
  // in the default case you return the old state unchanged
  return state;
})

请注意,我不建议使用此模式,因为您的更新函数不是纯函数。如果您可以添加任何关于当前用例的细节,也许我可以详细说明需要哪种模式。

您应该在setState方法中更新您的状态。 这是一个样本-

state = {
    number1 : Math.floor(Math.random() * 100),
    number2 : Math.floor(Math.random() * 100),
    number3 : Math.floor(Math.random() * 100),
    proposedAnswer: ''
}

calculateSum = () => {
  this.setState ({
    proposedAnswer:  Math.floor(Math.random() * 3) + this.state.number1 + this.state.number2 + this.state.number3
 )}
 console.log(state);
}

对于任何输入文本或div,您都可以在渲染方法中调用calculateSum方法。如果有帮助,请告诉我。

您应该在setState方法中更新状态。 这是一个样本-

state = {
    number1 : Math.floor(Math.random() * 100),
    number2 : Math.floor(Math.random() * 100),
    number3 : Math.floor(Math.random() * 100),
    proposedAnswer: ''
}

calculateSum = () => {
  this.setState ({
    proposedAnswer:  Math.floor(Math.random() * 3) + this.state.number1 + this.state.number2 + this.state.number3
 )}
 console.log(state);
}

对于任何输入文本或div,您都可以在渲染方法中调用calculateSum方法。如果这有帮助,请告诉我。

您尝试过吗?我相信所有的
this.state.numberX
属性都是未定义的,因为对象还没有初始化。不工作,indeds失败,带有
类型错误:无法读取未定义的
@NinoFiliu的属性'number1',这是我的错误。现在代码已经修复。你试过这个吗?我相信所有的
this.state.numberX
属性都是未定义的,因为对象还没有初始化。不工作,indeds失败,带有
类型错误:无法读取未定义的
@NinoFiliu的属性'number1',这是我的错误。现在代码已经修复。谢谢,伙计,现在它工作了!再次感谢汉克先生,现在它开始工作了!再次非常感谢