How to reload current page in ReactJS? in case of javascript we can write window.location.reload();
How to do the same in Reactjs? I’m able to add new data by UI. But without refreshing, I’m not able to see the list. I want it so that whenever I’m adding some data, it refreshes by itself.
onAddBucket() { let self = this; let getToken = localStorage.getItem('myToken'); var apiBaseUrl = "..."; let input = { "name" : this.state.fields["bucket_name"] } axios.defaults.headers.common['Authorization'] = getToken; axios.post(apiBaseUrl+'...',input) .then(function (response) { if(response.data.status == 200){ let result = self.state.buckets.concat(response.data.buckets) }else{ alert(response.data.message); } }) .catch(function (error) { console.log(error); }); }
Advertisement
Answer
Since React eventually boils down to plain old JavaScript, you can really place it anywhere! For instance, you could place it in a `componentDidMount()’ function in a React class.
For your edit, you may want to try something like this:
class Component extends React.Component { constructor(props) { super(props); this.onAddBucket = this.onAddBucket.bind(this); } componentWillMount() { this.setState({ buckets: {}, }) } componentDidMount() { this.onAddBucket(); } onAddBucket() { let self = this; let getToken = localStorage.getItem('myToken'); var apiBaseUrl = "..."; let input = { "name" : this.state.fields["bucket_name"] } axios.defaults.headers.common['Authorization'] = getToken; axios.post(apiBaseUrl+'...',input) .then(function (response) { if (response.data.status == 200) { this.setState({ buckets: this.state.buckets.concat(response.data.buckets), }); } else { alert(response.data.message); } }) .catch(function (error) { console.log(error); }); } render() { return ( {this.state.bucket} ); } }