Javascript 如何在 ReactJS 中重新加载当前页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47201919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reload current page in ReactJS?
提问by Riya Kapuria
How to reload current page in ReactJS? in case of javascript we can wrote window.location.reload();How to do the same in reactjs? I'm able to add new data by UI. But without refresh I'm not able to see the list. I want whenever I'm adding some data that time itself.
如何在 ReactJS 中重新加载当前页面?在 javascript 的情况下,我们可以写window.location.reload();如何在 reactjs 中做同样的事情?我可以通过 UI 添加新数据。但是如果不刷新,我将无法看到列表。每当我添加一些数据时,我都想要。
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);
});
}
采纳答案by Michael Jones
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.
由于 React 最终归结为普通的旧 JavaScript,因此您真的可以将它放在任何地方!例如,您可以将它放在componentDidMount()React 类中的 a 上。
For you 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}
);
}
}
回答by Nisal Edu
use this might help
使用这可能会有所帮助
window.location.reload();
回答by Tiago Alves
You can use window.location.reload();in your componentDidMount()lifecycle method. If you are using react-router, it has a refresh methodto do that.
您可以window.location.reload();在componentDidMount()生命周期方法中使用。如果您正在使用react-router,它有一个刷新方法来做到这一点。
Edit: If you want to do that after a data update, you might be looking to a re-rendernot a reloadand you can do that by using this.setState(). Here is a basic example of it to fire a re-renderafter data is fetched.
编辑:如果您想在数据更新后执行此操作,您可能正在寻找一个re-rendernot areload并且您可以使用this.setState()来做到这一点。这是re-render在获取数据后触发 a 的基本示例。
import React from 'react'
const ROOT_URL = 'https://jsonplaceholder.typicode.com';
const url = `${ROOT_URL}/users`;
class MyComponent extends React.Component {
state = {
users: null
}
componentDidMount() {
fetch(url)
.then(response => response.json())
.then(users => this.setState({users: users}));
}
render() {
const {users} = this.state;
if (users) {
return (
<ul>
{users.map(user => <li>{user.name}</li>)}
</ul>
)
} else {
return (<h1>Loading ...</h1>)
}
}
}
export default MyComponent;

