So my app has a table with data from the main page. If the user navigates to another page, input some data, then presses Home to go back to the main page, the table should be updated accordingly.
I'm trying to use componentWillUpdate() to re-render the page, but it causes an infinite loop of requests with the server.
class Dashboard extends Component{
constructor(props){
super(props);
this.state = {
table: null
}
}
componentWillMount(){
this.props.retrieveDashboard();
}
componentWillUpdate(nextProps, nextState){
nextProps.retrieveDashboard();
if (nextProps.users !== this.props.users){
this.setState({
table: nextProps.users
});
}
}
function mapStateToProps(state){
return {
users: state.auth.dash
};
}
export default connect(mapStateToProps,actions)(Dashboard);
I tried putting in a conditional to make sure it won't update then re-render a million times, but the conditional doesn't seem to do anything. Any idea?
EDIT: users is an array of JSON objects like this: [{object1}, {object2}]
This is the code for retrieveDashboard():
export function retrieveDashboard(){
return function(dispatch){
axios.get(`${SERVER_URL}/dashboard`)
.then(response => {
dispatch({type: USERS, payload: response.data});
})
.catch(() => console.log("error"))
}
}
I was under the impression that the data in the store is only updated by the axios call and dispatch, so I have to call it again if the database has been modified.