Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在重新加载或向后/向前重定向时保存React页面的状态?_Reactjs_Local Storage_React Navigation_React Props - Fatal编程技术网

Reactjs 如何在重新加载或向后/向前重定向时保存React页面的状态?

Reactjs 如何在重新加载或向后/向前重定向时保存React页面的状态?,reactjs,local-storage,react-navigation,react-props,Reactjs,Local Storage,React Navigation,React Props,下面是我的代码。我正在使用API并在当前页面上获取一些数据。现在,我想在重新加载页面或再次返回或前进时保存此页面的状态 在这里,我从上一页api获取featureGroupID,并将其存储在全局变量customerID中 我知道这是使用本地存储完成的,但由于我对Reactjs非常陌生,我不知道如何保存状态。有人能帮忙吗 class CustomerList扩展组件{ 状态={ 孤岛加载:是的, 用户:[], 错误:null, customerID:null }; componentDidMou

下面是我的代码。我正在使用API并在当前页面上获取一些数据。现在,我想在重新加载页面或再次返回或前进时保存此页面的状态

在这里,我从上一页api获取featureGroupID,并将其存储在全局变量customerID中

我知道这是使用本地存储完成的,但由于我对Reactjs非常陌生,我不知道如何保存状态。有人能帮忙吗

class CustomerList扩展组件{
状态={
孤岛加载:是的,
用户:[],
错误:null,
customerID:null
};
componentDidMount(){
取('http://localhost:8080/entity/getEntityByFeatureGroup/“+this.customerID)
.then(response=>response.json())
。然后(数据=>
这是我的国家({
用户:数据,
孤岛加载:false,
})
).catch(error=>this.setState({error,isLoading:false}));
}
render(){
var logTable=this.props;
控制台日志(日志表);
var customerColumnList=this.props;
this.customerID=customerColumnList.location.aboutProps.id.featureGroupID;
var headerName=customerColumnList.location.aboutProps.name.logTable.headerName;
const{isLoading,users,error}=this.state;

return(…..
您可以使用
localStorage.setItem
localStorage.getItem
访问本地存储。例如:

class CustomerList扩展组件{
状态={
孤岛加载:是的,
用户:[],
错误:null,
customerID:null
};
componentDidMount(){
如果(!localStorage.getItem('customerlist-data')){
取('http://localhost:8080/entity/getEntityByFeatureGroup/“+this.customerID)
.then(response=>response.json())
。然后(数据=>{
这是我的国家({
用户:数据,
孤岛加载:false,
});
setItem('customerlist-data',data);
}
).catch(error=>this.setState({error,isLoading:false}));
在此处输入代码}
}
render(){
var logTable=this.props;
控制台日志(日志表);
var customerColumnList=this.props;
this.customerID=customerColumnList.location.aboutProps.id.featureGroupID;
var headerName=customerColumnList.location.aboutProps.name.logTable.headerName;
const{isLoading,users,error}=this.state;

return(…..
您可以使用
localStorage.setItem
localStorage.getItem
访问本地存储。例如:

class CustomerList扩展组件{
状态={
孤岛加载:是的,
用户:[],
错误:null,
customerID:null
};
componentDidMount(){
如果(!localStorage.getItem('customerlist-data')){
取('http://localhost:8080/entity/getEntityByFeatureGroup/“+this.customerID)
.then(response=>response.json())
。然后(数据=>{
这是我的国家({
用户:数据,
孤岛加载:false,
});
setItem('customerlist-data',data);
}
).catch(error=>this.setState({error,isLoading:false}));
在此处输入代码}
}
render(){
var logTable=this.props;
控制台日志(日志表);
var customerColumnList=this.props;
this.customerID=customerColumnList.location.aboutProps.id.featureGroupID;
var headerName=customerColumnList.location.aboutProps.name.logTable.headerName;
const{isLoading,users,error}=this.state;

return(..
您可以存储数据+当前时间,并有条件地获取本地数据或再次从服务器获取数据

例如,我们可以决定,如果我们在本地存储了数据,但一个小时还没有过去,我们将显示本地数据,否则我们将从服务器获取数据

这里有一个粗略的例子

const storageKey = "myData";
const toHour = ms => Number((ms / (1000 * 60 * 60)).toFixed(2));

const storeDataLocally = data => {
  const dataObj = {
    date: Date.now(),
    data
  };
  localStorage.setItem(storageKey, JSON.stringify(dataObj));
};

const getDataLocally = () => {
  const dataObj = localStorage.getItem(storageKey);
  return JSON.parse(dataObj);
};

class App extends React.Component {
  state = {
    data: []
  };

  getDataFromServer = () => {
    console.log("from server");
    fetch("https://jsonplaceholder.typicode.com/users")
      .then(response => response.json())
      .then(data => {
        storeDataLocally(data);
        this.setState({ data });
      });
  };

  componentDidMount() {
    const localObj = getDataLocally();
    let shouldGetDataFromserver = false;
    if (localObj) {
      const isOneHourAgo =
        toHour(new Date()) - toHour(Number(localObj.date)) > 1;
      if (isOneHourAgo) {
        shouldGetDataFromserver = true;
      }
    } else {
      shouldGetDataFromserver = true;
    }

    shouldGetDataFromserver
      ? this.getDataFromServer()
      : this.setState({ data: localObj.data });
  }

  render() {
    const { data } = this.state;
    return (
      <div>
        {data.map(user => (
          <div key={user.id}>{user.name}</div>
        ))}
      </div>
    );
  }
}
const storageKey=“myData”;
常数toHour=ms=>Number((ms/(1000*60*60)).toFixed(2));
const storedatalocal=数据=>{
常数dataObj={
日期:date.now(),
数据
};
setItem(storageKey,JSON.stringify(dataObj));
};
const getdatalocal=()=>{
const dataObj=localStorage.getItem(storageKey);
返回JSON.parse(dataObj);
};
类应用程序扩展了React.Component{
状态={
数据:[]
};
getDataFromServer=()=>{
日志(“来自服务器”);
取回(“https://jsonplaceholder.typicode.com/users")
.then(response=>response.json())
。然后(数据=>{
本地存储数据(数据);
this.setState({data});
});
};
componentDidMount(){
const localObj=getdatalocal();
让shouldGetDataFromserver=false;
if(localObj){
康斯特伊索内胡拉戈酒店=
TOHOURE(new Date())-TOHOURE(Number(localObj.Date))>1;
if(isOneHourAgo){
shouldGetDataFromserver=true;
}
}否则{
shouldGetDataFromserver=true;
}
应该从服务器获取数据
?这是一个.getDataFromServer()
:this.setState({data:localObj.data});
}
render(){
const{data}=this.state;
返回(
{data.map(用户=>(
{user.name}
))}
);
}
}

您可以存储数据+当前时间,并有条件地获取本地数据或再次从服务器获取数据

例如,我们可以决定,如果我们在本地存储了数据,但一个小时还没有过去,我们将显示本地数据,否则我们将从服务器获取数据

这里有一个粗略的例子

const storageKey = "myData";
const toHour = ms => Number((ms / (1000 * 60 * 60)).toFixed(2));

const storeDataLocally = data => {
  const dataObj = {
    date: Date.now(),
    data
  };
  localStorage.setItem(storageKey, JSON.stringify(dataObj));
};

const getDataLocally = () => {
  const dataObj = localStorage.getItem(storageKey);
  return JSON.parse(dataObj);
};

class App extends React.Component {
  state = {
    data: []
  };

  getDataFromServer = () => {
    console.log("from server");
    fetch("https://jsonplaceholder.typicode.com/users")
      .then(response => response.json())
      .then(data => {
        storeDataLocally(data);
        this.setState({ data });
      });
  };

  componentDidMount() {
    const localObj = getDataLocally();
    let shouldGetDataFromserver = false;
    if (localObj) {
      const isOneHourAgo =
        toHour(new Date()) - toHour(Number(localObj.date)) > 1;
      if (isOneHourAgo) {
        shouldGetDataFromserver = true;
      }
    } else {
      shouldGetDataFromserver = true;
    }

    shouldGetDataFromserver
      ? this.getDataFromServer()
      : this.setState({ data: localObj.data });
  }

  render() {
    const { data } = this.state;
    return (
      <div>
        {data.map(user => (
          <div key={user.id}>{user.name}</div>
        ))}
      </div>
    );
  }
}
const storageKey=“myData”;
常数toHour=ms=>Number((ms/(1000*60*60)).toFixed(2));
const storedatalocal=数据=>{
常数dataObj={
日期:date.now(),
数据
};
setItem(storageKey,JSON.stringify(dataObj));
};
const getdatalocal=()=>{
const dataObj=localStorage.getItem(storageKey);
返回JSON.parse(dataObj);
};
类应用程序扩展了React.Component{
状态={
数据:[]
};
getDataFromServer=()=>{
console.log(“fro