I want to use redux but still control what's being rendered in bootstrap-react-table through my local state. So, in componentDidMount I'm fetching my data and when I try to filter through a button click I setState to a field named "itemsToDisplay", which I then want to serve as data to the table. But the click only works the second time around, and when I log the state in render/componentDidMount it's undefined.
I've tried different conditions for rendering the items in the table's data field prop, yet unsuccessful. I've also tried setting state in componentWillMount.
class Deliveries extends Component {
constructor(props) {
super(props);
this.state = {
itemsToDisplay: this.props.deliveries,
};
}
componentDidMount() {
this.props.getDeliveries();
this.setState(prev => {
return {
...prev,
itemsToDisplay: this.props.deliveries,
}
})
}
filterUndelivered = () => {
this.props.filterUndelivered();
this.setState(prev => {
return {
...prev,
itemsToDisplay: this.props.undelivered_items,
}
})
};
getFilters = () => {
return (
<div>
<Button className="m-1" color="primary">By time</Button>
<Button className="m-1" color="primary" onClick={ this.filterUndelivered }>Not delivered</Button>
</div>
);
};
render() {
const { itemsToDisplay } = this.state;
const options = {
insertBtn: this.createCustomInsertButton,
searchField: this.createCustomSearchField,
};
return (
<div className="animated fadeIn">
<div className="d-flex justify-content-between">
<div className="font-weight-bold mt-2">Deliveries Monitor</div>
<div className="d-flex justify-content-end">
{this.getFilters()}
</div>
</div>
<BootstrapTable
data={ itemsToDisplay ? itemsToDisplay : this.props.deliveries }
version="4"
search
hover
pagination
bordered={false}
headerStyle={{ background: "#20a8d8" }}
bodyStyle={{ cursor: "pointer"}}
options={ options }
>
<TableHeaderColumn isKey dataField="delivery_id">Delivery#</TableHeaderColumn>
<TableHeaderColumn dataField="seller">Seller</TableHeaderColumn>
<TableHeaderColumn dataField="address">Address</TableHeaderColumn>
<TableHeaderColumn dataField="site">Site</TableHeaderColumn>
<TableHeaderColumn dataField="end_of_window">End of window</TableHeaderColumn>
<TableHeaderColumn dataField="due_date">Due date</TableHeaderColumn>
<TableHeaderColumn dataField="status">Status</TableHeaderColumn>
</BootstrapTable>
</div>
)
}
}
const mapStateToProps = state => {
return {
deliveries: state.deliveriesReducer.deliveries,
undelivered_items: state.deliveriesReducer.undelivered_items,
}
};
const mapDispatchToProps = (dispatch) => {
return bindActionCreators({ getDeliveries, filterUndelivered }, dispatch);
};
export default connect(mapStateToProps, mapDispatchToProps)(Deliveries);
The expected result is to be able to filter on the first click, when the condition to the data prop is correct.
Try modifying your code like below :
filterUndelivered = () => {
this.props.filterUndelivered();
};
componentWillReceiveProps = nextProps => {
if (nextProps.undelivered_items !== undefined) {
this.setState({ itemsToDisplay: nextProps.undelivered_items });
}
};
Also install Redux dev tools in browser and analyse application state .It may give you more insight about the issue.
Chrome: https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd
Firefox: https://addons.mozilla.org/en-US/firefox/addon/reduxdevtools/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With