Want to reload current page in ReactJS? Since React eventually boils down to plain old JavaScript, you can really place it anywhere! For instance, you could place it on a componentDidMount() in a React class.
For you edit, you may want to try something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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} ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.