简体   繁体   English

如何在同一个 React 组件中管理多个 setState 调用?

[英]How can I manage multiple setState calls in the same React component?

I am using an npm package for creating a data grid.我正在使用 npm package 来创建数据网格。 This datagrid is a class component and comes with rows as state.此数据网格是 class 组件,并带有 state 行。 I am also trying to add inputs above the data grid that allow users to change the values of these inputs while also changing the values of the data grid.我还尝试在数据网格上方添加输入,以允许用户更改这些输入的值,同时更改数据网格的值。 However, only the data grid values are changing.但是,只有数据网格值发生了变化。 The inputs are NOT changing so the updateFormData function isn't working输入没有改变,因此 updateFormData function 不起作用

Here is the code这是代码

import ReactDOM from "react-dom";
import ReactDataGrid from "react-data-grid";
import "./SellPressure.css";

const columns = [
  { key: "layer", name: "Layer", editable: false },
  { key: "electric", name: "Elec. Rate", editable: true, resizable: true },
  { key: "percentHash", name: "% Hash", editable: true },
  { key: "percentHashAfterBlowout", name: "Adj. % Hash", editable: false },
  { key: "oldGen", name: "S9 Cost", editable: false },
  { key: "newGen", name: "S17 Cost", editable: false },
  { key: "OldGenTier", name: "S9 %", editable: false },
  { key: "NewGenTier", name: "S17 %", editable: false },
  { key: "OldGenTierBlowout", name: "Adj. S9%", editable: false },
  { key: "NewGenTierBlowout", name: "Adj. S17%", editable: false },
  { key: "percentSold", name: "% Sold", editable: false },
  { key: "BTCSold", name: "BTC Sold", editable: false },
];


const rows = [
  { layer: 1, electric: .020, percentHash: 5, percentHashAfterBlowout: 0, oldGen: 0, newGen: 0,
  OldGenTier: 0, NewGenTier: 0, OldGenTierBlowout: 0, NewGenTierBlowout: 0, percentSold: 0, BTCSold: 0
}
];

// require complex functions to determine their value
// percentHashAfterBlowout,
// OldGenTierBlowout,
// percentSold




class Example extends React.Component {
  // state = { rows };

  state = {
      rows: [...rows],
      rewardYo: "",
      btcPriceYo: this.props.btcPrice,
      difficultyYo: this.props.difficulty,
      s9Hash: 13.5,
      s17Hash: 50,
      s9Power: 1400,
      s17Power: 2100 
  }



  updateFormData = event => {
    this.setState({
      [event.target.name]: event.target.value
    });
  };


  onGridRowsUpdated = ({ fromRow, toRow, updated }) => {
    this.setState(state => {
      const rows = state.rows.slice();
      for (let i = fromRow; i <= toRow; i++) {
        rows[i] = { ...rows[i], ...updated };
      }
      return { rows };
    });
  };




  render() {
    return (

<div>

        <input
        value={this.state.rewardYo}
        onChange={this.updateFormData}
        type="number"
        name="rewardYo"
        required
        />

<input
        value={this.state.btcPriceYo}
        onChange={this.updateFormData}
        type="btcPriceYo"
        name="btcPriceYo"
        required 
        />

  <div>
      <ReactDataGrid
        columns={columns}
        rowGetter={i => this.state.rows[i]}
        rowsCount={1}
        onGridRowsUpdated={this.onGridRowsUpdated}
        enableCellSelect={true}
        cellRangeSelection={{
          onStart: args => console.log(rows),
          onUpdate: args => console.log(rows),
          onComplete: args => console.log(rows)
        }}
      />
  </div>
</div>
    );
  }
}

export default Example;```

The inputs are not changing because the updateDataForm need some changes:输入没有改变,因为 updateDataForm 需要一些改变:

updateFormData = (event, stateName) => {
    this.setState({
      [stateName]: event.target.value
    });
  };

Then you call it like this:然后你这样称呼它:

onChange={(e) => this.updateFormData(e, 'theStateYouWantToChange')}

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

相关问题 我如何在 React 的卸载组件中设置状态 - how can I setState in unmount Component in React 如何在 React 的同一组件中使用按钮设置另一个输入值? - How can I setState() another input value with a button in the same component in React? 如何在同一页面上的多个实例上使用React组件? - How can I use a React component on multiple instances on the same page? React方法中的多个setState()调用:如何使其“同步”工作 - Multiple setState() calls in a React method: How to make it work “synchronously” 我如何通过RxJS限制对React组件方法的调用 - How can I throttle calls of methods of a react component via RxJS 使用setTimeout的setState时如何重新渲染react-component? - How can I re-render react-component when I setState using setTimeout? React是否会在多个功能性setState调用之间呈现? - Will React render in between multiple functional setState calls? 如何在本机反应中重用 function 和 this.setState 和其他组件 function? - How can I reuse a function with this.setState and other component function in react native? 如何让 setState 在功能性 React 组件的事件处理程序中工作 - How can i get setState to work in an event handler in a functional React component Semantic UI React:同一个组件中多个 useEffect 调用的问题 - Semantic UI React: Problem with multiple useEffect calls in the same component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM