简体   繁体   English

在 onclick 中调用 function 后 React.js 不增加

[英]React.js not incrementing after function call in onclick

I need to build a table in order to organize some data.我需要建立一个表格来组织一些数据。 I'm using the "onclick" function in order to call a separate function, which is supposed to increment a state variable up by one.我正在使用“onclick”function 来调用单独的 function,这应该将 state 变量增加一。 My Chrome Devtools isn't giving me any errors but also isn't updating the stock variable.我的 Chrome Devtools 没有给我任何错误,但也没有更新 stock 变量。 I'm not sure how to get the state to update and display.我不确定如何让 state 更新和显示。 Here's my source code:这是我的源代码:

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            cars: [
                {
                    manufacturer: "Toyota",
                    model: "Rav4",
                    year: 2008,
                    stock: 3,
                    price: 8500
                },
    
                {
                    manufacturer: "Toyota",
                    model: "Camry",
                    year: 2009,
                    stock: 2,
                    price: 6500
                },
    
                {
                    manufacturer: "Toyota",
                    model: "Tacoma",
                    year: 2016,
                    stock: 1,
                    price: 22000
                },
    
                {
                    manufacturer: "BMW",
                    model: "i3",
                    year: 2012,
                    stock: 5,
                    price: 12000
                },
    
            ]
        };
        this.renderCar = this.renderRow.bind(this);
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
        this.setState(() => {
            return {stock: this.stock + 1}
        })
    }

    renderRow(car, index) {
        return (
            <tr key={index}>
                <td key={car.manufacturer}>{car.manufacturer}</td>
                <td key={car.model}>{car.model}</td>
                <td key={car.year}>{car.year}</td>
                <td key={car.stock}>{car.stock}</td>
                <td key={car.price}>${car.price}.00</td>
                <td key={index}><input type="button" onClick={car.handleClick} value="Increment" /></td>
            </tr>
        )
    }

    render() {
        return (
            <div>
            <table>
                <thead>
                <tr>
                    <th>Manufacturer</th>
                    <th>Model</th>
                    <th>Year</th>
                    <th>Stock</th>
                    <th>Price</th>
                    <th>Option</th>
                </tr>
                </thead>
                <tbody>
                    {this.state.cars.map(this.renderRow)}
                </tbody>
            </table>
            </div>
        );
    };
}

ReactDOM.render(<App />, document.getElementById("app"))

I'd make a separate component for the row, so that you can easily update that component to increment the stock value in state:我会为该行创建一个单独的组件,以便您可以轻松地更新该组件以增加 state 中的stock值:

 class App extends React.Component { constructor(props) { super(props); this.state = { cars: [ { manufacturer: "Toyota", model: "Rav4", year: 2008, stock: 3, price: 8500 }, { manufacturer: "Toyota", model: "Camry", year: 2009, stock: 2, price: 6500 }, { manufacturer: "Toyota", model: "Tacoma", year: 2016, stock: 1, price: 22000 }, { manufacturer: "BMW", model: "i3", year: 2012, stock: 5, price: 12000 }, ] }; } render() { return ( <div> <table> <thead> <tr> <th>Manufacturer</th> <th>Model</th> <th>Year</th> <th>Stock</th> <th>Price</th> <th>Option</th> </tr> </thead> <tbody> {this.state.cars.map( (car, i) => <Row car={car} key={i} /> )} </tbody> </table> </div> ); }; } const Row = ({ car }) => { const [stock, setStock] = React.useState(car.stock); return ( <tr> <td>{car.manufacturer}</td> <td>{car.model}</td> <td>{car.year}</td> <td>{stock}</td> <td>${car.price}.00</td> <td><input type="button" onClick={() => setStock(stock + 1)} value="Increment" /></td> </tr> ); } ReactDOM.render(<App />, document.getElementById("app"))
 <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id='app'></div>

You could put it all in one component if you had to, but it'd be a bit cumbersome.如果必须,您可以将它们全部放在一个组件中,但这会有点麻烦。 While rendering, you'd have to keep track of the render index of a row and pass that along to the click handler, then immutably update the stock property in the state array at that index.在渲染时,您必须跟踪一行的渲染索引并将其传递给单击处理程序,然后在该索引处不可变地更新 state 数组中的stock属性。 A separate component is easier.单独的组件更容易。

   handleClick(e){
      const index = Number(e.currentTarget.value);
      this.setState(this.state.cars.map(car, i)=> {
        return i === index ? {...car, stock: car.stock + 1}: car 
      })
   }
 
   renderRow(){
    ....
       <input type="button" onClick={this.handleClick} value={index} />
    ...
   }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM