简体   繁体   English

在对 state 中的元素进行排序后,反应列表不会重新呈现表

[英]React list isn't re-rendering table after sorting elements in state

I'm having issues with React not rendering after sorting the table I have.在对我拥有的表格进行排序后,我遇到了 React 未呈现的问题。 It seems to update the state variable and I'm using setState, I just have no idea why it's not showing the new updated data.似乎更新了 state 变量,我正在使用 setState,我只是不知道为什么它没有显示新的更新数据。 Here's my code这是我的代码

class CarRow extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            manufacturer: this.props.passedCar.manufacturer,
            model: this.props.passedCar.model,
            year: this.props.passedCar.year,
            stock: this.props.passedCar.stock,
            price: this.props.passedCar.price,
        }

        this.state.price = this.state.price.toLocaleString(undefined, {maximumFractionDigits: 2})
    }

    handleCount(value) {
        this.setState((prevState) => ({stock: prevState.stock + value}));
    }

    render() {
        return (<tr>
            <td>{this.state.manufacturer}</td>
            <td>{this.state.model}</td>
            <td>{this.state.year}</td>
            <td>{this.state.stock}</td>
            <td>${this.state.price}</td>
            <td>
                <button onClick={() =>
                    this.handleCount(1)}>
                    Increment
                </button>
            </td>
        </tr>)
    }
}


class App extends React.Component {

    constructor(props) {
        super(props);

        this.state = {
            descending: true,
            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": "Dodge",
                    "model": "Charger",
                    "year": 2013,
                    "stock": 2,
                    "price": 16000
                },

                {
                    "manufacturer": "Ford",
                    "model": "Mustang",
                    "year": 2009,
                    "stock": 1,
                    "price": 8000
                },

            ]
        };
    }

    sortCars() {
        var carsSorted = JSON.parse(JSON.stringify(this.state.cars));
        carsSorted.sort((a, b) => (this.state.descending ? b.price - a.price : a.price - b.price));
        this.setState({cars: carsSorted});
        this.setState({descending: !this.state.descending});
    }

    render() {
        return (
            <table>
                <thead>
                <tr>
                    <th>manufacturer</th>
                    <th>model</th>
                    <th>year</th>
                    <th>stock</th>
                    <th onClick={() =>
                        this.sortCars()}>price
                    </th>
                    <th>Option</th>
                </tr>
                </thead>
                <tbody>
                {this.state.cars.map((car) => {
                    return <CarRow passedCar={car}/>;
                })}
                </tbody>
            </table>
        );
    };
}

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

I've tried also creating a separate item for the table component, but it didn't seem to work.我也尝试为表格组件创建一个单独的项目,但它似乎没有用。 I'm pretty new to react, and I don't know alot about it.我对反应很陌生,对此我不太了解。

If I could get some help with this, that would be great, thanks.如果我能在这方面得到一些帮助,那就太好了,谢谢。

You need to give your child CarRow components a key when iterating over them so they get re-rendered properly.您需要在对 CarRow 子组件进行迭代时为其提供一个键,以便正确地重新渲染它们。 Find something unique that'll identify a row, and set that as the key.找到可以识别行的唯一内容,并将其设置为键。 For example, you could put together the model and the year:例如,您可以将 model 和年份放在一起:

return <CarRow key={car.model + '_' + car.year} passedCar={car}/>;

Then they'll appear sorted in the proper direction as desired when rendering again.然后,在再次渲染时,它们将按照需要按正确的方向排序。

 class CarRow extends React.Component { constructor(props) { super(props); this.state = { manufacturer: this.props.passedCar.manufacturer, model: this.props.passedCar.model, year: this.props.passedCar.year, stock: this.props.passedCar.stock, price: this.props.passedCar.price, } this.state.price = this.state.price.toLocaleString(undefined, {maximumFractionDigits: 2}) } handleCount(value) { this.setState((prevState) => ({stock: prevState.stock + value})); } render() { return (<tr> <td>{this.state.manufacturer}</td> <td>{this.state.model}</td> <td>{this.state.year}</td> <td>{this.state.stock}</td> <td>${this.state.price}</td> <td> <button onClick={() => this.handleCount(1)}> Increment </button> </td> </tr>) } } class App extends React.Component { constructor(props) { super(props); this.state = { descending: true, 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": "Dodge", "model": "Charger", "year": 2013, "stock": 2, "price": 16000 }, { "manufacturer": "Ford", "model": "Mustang", "year": 2009, "stock": 1, "price": 8000 }, ] }; } sortCars() { var carsSorted = JSON.parse(JSON.stringify(this.state.cars)); carsSorted.sort((a, b) => (this.state.descending? b.price - a.price: a.price - b.price)); this.setState({cars: carsSorted}); this.setState({descending: .this.state;descending}). } render() { return ( <table> <thead> <tr> <th>manufacturer</th> <th>model</th> <th>year</th> <th>stock</th> <th onClick={() => this.sortCars()}>price </th> <th>Option</th> </tr> </thead> <tbody> {this.state.cars.map((car) => { return <CarRow key={car.model + '_' + car;year} passedCar={car}/>; })} </tbody> </table> ); }. } ReactDOM,render(<App />. document.querySelector(';react'));
 <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 class='react'></div>

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

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