简体   繁体   English

如何使用来自localStorage的数据渲染React组件?

[英]How to Render React Components with Data from localStorage?

I'm building a React app where a lot of the content of the react app is pulled from the localStorage. 我正在构建一个React应用程序,其中React应用程序的许多内容都是从localStorage提取的。 Rather than using a database, when the app is run, the first thing that is done is the localStorage is populated with the data, then the HTML is built from the JS using the data from the localStorage.... 在运行该应用程序时,与其使用数据库,不如使用数据库,首先要做的是用数据填充localStorage,然后使用localStorage的数据从JS构建HTML。

But now with React, I'm starting to move from hard coding the information in, to using it from the localStorage. 但是现在有了React,我开始从对信息进行硬编码转变为从localStorage使用信息。 However, it seems the react components are rendered before the JS that loads the localStorage. 但是,似乎React组件是在加载localStorage的JS之前呈现的。 Using console.log, I found that the React rendering was done before anything else. 使用console.log,我发现React渲染首先完成。 How would I go about making sure the HTML (from the react components) isn't implemented until AFTER the JS is done that builds the localStorage? 我将如何确保在完成构建localStorage的JS之后才实现HTML(来自react组件)?

MAIN APP COMPONENT 主要应用程式组件

class App extends React.Component {

  constructor(props) {
    super(props);
    console.log("Constructing App");
  }

  render() {
    return (
    <div>
      <Navigation {this.title: "Something based on localStorage"/>
     </div>
    )}
 }

NAVIGATION COMPONENT 导航组件

function Navigation(props) {
  console.log("Constructing Nav");

  return ("stuff here pulled from props like props.title")
}

You can use the local storage getItem method - localStorage.getItem('myData'); 您可以使用本地存储的getItem 方法 localStorage.getItem('myData');

class App extends React.Component {

  constructor(props) {
    super(props);
    state: {
      myData: {}      
    }  
  }

  componentDidMount(){
    const myData = localStorage.getItem('myData');
    // set the state with the data
    this.setState({myData});
  }

  render() {
    return (
    <div>
      <Navigation title={this.state.myData} />
     </div>
    )}
 }

Let me try to revise your code some: 让我尝试修改您的代码:

class App extends React.Component {
      constructor(props) {
        super(props);
        console.log("Constructing App");
        this.state={
          fromLocal:''
        }
      }
      componentDidMount(){
        let newSearchColumns = localStorage.getItem('searchColumns');
        this.setState({fromLocal: newSearchColumns})
      }

      render() {
        return (
        <div>
          <Navigation title= {this.state.fromLocal}/>
         </div>
        )}
     }

You should research the following: Lifecycle methods and Local Storage 您应该研究以下内容: 生命周期方法本地存储

The default methods of a React Component are run in a certain sequence ( the "lifecycle" of the component ). React Component的默认方法按特定顺序( 该组件的“生命周期” )运行。

If you have operations you want to perform, like building localStorage, you can place them earlier in the sequence. 如果您要执行某些操作(例如构建localStorage),则可以将它们放在序列的前面。

Initialization and Mounting both occur before rendering, so you can build localStorage in either of those phases. 初始化和挂载都在渲染之前进行,因此您可以在这两个阶段之一中构建localStorage。 Specifically, try overriding the componentWillMount() method. 具体来说,请尝试重写componentWillMount()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM