[英]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.