reactjs:setState 在调用一次的函数中被调用两次?为什么?

IT技术 javascript reactjs setstate
2021-05-11 23:18:11

编辑:由于代码片段不会重现该错误 - 这是 github 存储库的链接:(代码远未完成)

https://github.com/altruios/clicker-game

我现在已经在两台计算机上运行了它 - 两者都具有代码片段未显示的相同行为。

//interestingly enough, this works just fine, where the same code I run locally has the doubling.
//when I comment out ALL other code except for this code I STILL get the error locally
//at this point the only difference is import export of components... here they are in one file.
//below is original code from file (
/* 
FILE::::Clicker.js
 
import React from 'react';

function Clicker(props)	
	{
	return(
		<div>
		{props.name}
		<button 
			name={props.name} 
			onClick={props.HandleClick} 
			data-target={props.subjectsOfIncrease}> 
				{props.name} {props.value}
	
		</button>
		</div>


		)
}

export default Clicker;

FILE:: Resouce.js

import React from 'react';
function Resource(props)	
	{
	return(
		<div>
		{props.name} and  {props.amount || 0}

		</div>


		)
}

export default Resource;



*/
//besides the import/export and seprate files - code is the same. it works in here, does not work locally on my machine.

const gameData = {
  clickerData: [{
    name: "grey",
    subjectsOfIncrease: ["grey"],
    isUnlocked: true,
    value: 1
  }],
  resourceData: [{
    name: "grey",
    resouceMax: 100,
    isUnlocked: true,
    changePerTick: 0,
    counterTillStopped: 100,
    amount: 0
  }]
}
class App extends React.Component {
    constructor() {
      super();
      this.state = {
        resources: gameData.resourceData,
        clickers: gameData.clickerData
      };
      this.gainResource = this.gainResource.bind(this);
    }
    gainResource(event) {
      console.count("gain button");
      const name = event.target.name;
      this.setState((prevState) => {
        const newResources = prevState.resources.map(resource => {
          if (resource.name === name) {
            resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
          }
          return resource;
        });
        console.log(prevState.resources.find(item => item.name === name).amount, "old");
        console.log(newResources.find(item => item.name === name).amount, "new");
        return {
          resources: newResources
        }
      });
    }
    render() {
      const resources = this.state.resources.map(resourceData => {
          return (
            <Resource 
              name = {resourceData.name}
              resouceMax = {resourceData.resourceMax}
              isUnlocked = {resourceData.isUnlocked}
              changePerTick = {resourceData.changePerTick}
              counterTillStopped = {resourceData.countTillStopped}
              amount = {resourceData.amount}
              key = {resourceData.name}
            />
          )
      })

      const clickers = this.state.clickers.map(clickerData => {
          return ( 
            <Clicker 
              name = {clickerData.name}
              HandleClick = {this.gainResource}
              value = {clickerData.amount}
              key = {clickerData.name}
            />
          )
    })

    return (
          <div className = "App" > 
            {resources} 
           {clickers} 
          </div>
   )
 }
}
function Resource(props) {
      return  <div >  {props.name} and {props.amount || 0} </div>
}

function Clicker(props) {
      return ( 
        <div > {props.name} 
          <button name = {props.name}  onClick = {props.HandleClick}>
            {props.name} {props.value}
          </button> 
        </div>
     )
}
const root = document.getElementById('root');
ReactDOM.render(  <App / >,root );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

所以我正在构建一个点击游戏来学习react,我不明白为什么这段代码的行为方式是这样的:

在主应用程序中,我有这个功能:


  gainResource(event)
    {
    console.count("gain button");
    const name = event.target.name;
    this.setState( (prevState)=>
      {
      const newResources =  prevState.resources.map(resource=>
        {
        if(resource.name === name)  
          {
          resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
        }  
        return resource;
      });
      console.log(prevState.resources.find(item=>item.name===name).amount, "old");
      console.log(newResources.find(item=>item.name===name).amount, "new");
      return {resources: newResources}
    });
  }

该 console.count 运行一次......但我得到了 2 个“新旧”对。好像 setState 在这个只运行一次的函数中运行了两次?

console.output 是:

App.js:64 gain button: 1
App.js:76 1 "old"
App.js:77 1 "new"
App.js:76 2 "old"
App.js:77 2 "new"

所以看起来这个函数正在运行一次。但是设置状态正在运行两次?

症状是它增加了 2。但是数量的初始状态是 0,而不是 1,如在 gamedata.json 中所见

    resourceData:
        [
            {   
            name:"grey",
            resouceMax:100,
            isUnlocked:true,
            changePerTick:0,
            counterTillStopped:100,
            amount:0
            },{etc},{},{}],
        clickerData:
        [
            {
            name:"grey",
            subjectsOfIncrease:["grey"],
            isUnlocked:true,
            value:1
           },{etc},{},{}]


我不认为我将要使用的其余代码与这种行为相关,但我还不知道react,所以我不知道我错过了什么:但这就是我的方式生成点击器按钮:

const clickers = this.state.clickers.map(clickerData=>
      {
      return(
        <Clicker
          name={clickerData.name}  
          HandleClick = {this.gainResource}
          value = {clickerData.amount}
          key={clickerData.name}

        />

        )
    })

在 clicker.js 功能组件中,我只是返回这个:

        <div>
        {props.name}
        <button name={props.name} onClick={props.HandleClick}>
                {props.name} {props.value}
        </button>
        </div>

该函数在构造函数中绑定到 this ......我不明白为什么它在一个被调用一次的函数中运行 setState 两次。

我也试过:


        <div>
        {props.name}
        <button name={props.name} onClick={()=>props.HandleClick}> //anon function results in no output
                {props.name} {props.value}
        </button>
        </div>
4个回答

最佳答案:

我正在使用 create-react-app。并且我的 App 组件被包装在严格模式下......它触发 setState 两次......这完美地解释了为什么这不能在代码片段中重现,以及为什么该函数被调用一次,而 setState 被调用两次。

删除严格模式完全解决了这个问题。

这是封装在<React.Strict>组件中的setState(callback)方法的预期行为

回调执行两次以确保它不会直接改变状态。

根据https : //github.com/facebook/react/issues/12856#issuecomment-390206425


在代码片段中,您创建了一个新数组,但其中的对象仍然相同:

const newResources = lastResources.map(resource => {
  if(resource.name === name){
    resource.amount = Number(resource.amount) + 1 
  }  
  return resource;
}

您必须单独复制每个对象:

const newResources = lastResources.map(resource => {
  const newObject = Object.assign({}, resource)
  if(resource.name === name){
    newObject.amount = Number(newObject.amount) + 1 
  }  
  return newObject;
}

只要您没有向我们提供一个可运行的示例,我就会怀疑会发生什么,让我们看看它是否有效。

我能看到的是在gainResource函数中,特别是在这一行resource.amount = Number(resource.amount) + 1 中,您尝试在不使用React 文档不推荐的 setState 的情况下更新状态

请先尝试分配一个const myRessource = ressource 然后返回myRessource


  gainResource(event)
    {
    console.count("gain button");
    const name = event.target.name;
    this.setState( (prevState)=>
      {
      const newResources =  prevState.resources.map(resource=>
        {
       const myRessource = ressource;
        if(myRessource.name === name)  
          {
          myRessource.amount = Number(myRessource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
        }  
        return myRessource;
      });
      console.log(prevState.resources.find(item=>item.name===name).amount, "old");
      console.log(newResources.find(item=>item.name===name).amount, "new");
      return {resources: newResources}
    });
  }

好吧......所以在一些头发拉扯之后......我找到了一种有效的方法......但我不认为这是'最佳实践'但是当我写这个时它现在对我有用:

  gainResource(event)
    {
    const name = event.target.name;

    const lastResources =  this.state.resources.slice();
      const newResources = lastResources.map(resource=>
        {
        if(resource.name === name)  
          {
          resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
        }  
        return resource;
      });

    this.setState({resources: newResources});
  }

对比


  gainResource(event)
    {
    console.count("gain button");
    const name = event.target.name;
    this.setState( (prevState)=>
      {
      const newResources =  prevState.resources.map(resource=>
        {
        if(resource.name === name)  
          {
          resource.amount = Number(resource.amount) + 1 //Number(prevState.clickers.find(x=>x.name===name).value)
        }  
        return resource;
      });
      console.log(prevState.resources.find(item=>item.name===name).amount, "old");
      console.log(newResources.find(item=>item.name===name).amount, "new");
      return {resources: newResources}
    });
  }

没有 prevState 函数的 setState 被调用一次......而使用 prevState 它被调用两次......为什么?

所以我仍然不明白为什么 setState 使用带有 prevState 的函数会在一个只调用一次的函数中导致两个函数调用......我已经读到我应该使用 prev 状态作为 this.state.resources.slice(); 只是拍摄“不定时快照”并且可能不可靠。这是真的……还是这种方法可以接受?

这是对其他为此而苦苦挣扎的人的回答。希望在对可能发生的事情有所启发后,可以发布更好的答案。