javascriptreactjsfirebasefirebase-realtime-databasereact-bootstrap-table

React-Bootstrap-Table: Each child in a list should have a unique "key" prop


I've been trying to create a react-bootstrap-table2 but I get the following warning: Each child in a list should have a unique "key" prop.

Here is my code:

export const columns = [
  {
    dataField: "timestamps",
    text: "Timestamp",
  },
];

class Table extends Component {
  constructor(props) {
    super(props);
    this.state = { timestamps: [] };
  }
  componentDidMount() {
    const database = db.ref().child("timestamped_measures");
    database.on("value", (ts_measures) => {
      const timestamps = [];
      const columns = [{ dataField: "timestamps", text: "Timestamp" }];
      ts_measures.forEach((ts_measure) => {
        timestamps.push(ts_measure.val().timestamp);
      });
      console.log(timestamps);
      this.setState((prevState) => {
        return { timestamps: [...prevState.timestamps, ...timestamps] };
      });
    });
  }

  render() {
    return (
      <div className="App">
        <BootstrapTable
          keyField="timestamps"
          data={this.state.timestamps.map((item) => ({ item }))}
          columns={columns}
          pagination={paginationFactory()}
        />
      </div>
    );
  }
}
export default Table;

Here is the console with the list of data I am trying to display

So my question is how to give each child in a list an unique key.

Any help would be appreciated!


Solution

  • You are passing array of integers instead of array of objects that has "timestamp" property.

    export const columns = [
     {
       dataField: "timestamp",
       text: "Timestamp",
     },
    ];
    
    class Table extends Component {
      constructor(props) {
       super(props);
       this.state = { timestamps: [] };
      }
      
    componentDidMount() {
    const database = db.ref().child("timestamped_measures");
    database.on("value", (ts_measures) => {
      const timestamps = [];
      ts_measures.forEach((ts_measure) => {
        timestamps.push({ timestamp: ts_measure.val().timestamp });
      });
      console.log(timestamps);
      this.setState((prevState) => {
        return { timestamps: [...prevState.timestamps, ...timestamps] };
      });
    });
    }
    
     render() {
        return (
          <div className="App">
            <BootstrapTable
              keyField="timestamp"
              data={this.state.timestamps}
              columns={columns}
              pagination={paginationFactory()}
             />
           </div>
        );
        }
      }
     export default Table;