简体   繁体   English

React-在渲染之前获取数据(最佳解决方案)

[英]React - fetching the data before the render (best solution)

So, I have an app that user can choose several settings in. Like number format or a currency that will be displayed by default. 因此,我有一个应用程序,用户可以在其中选择多个设置。例如数字格式或默认显示的货币。

For that there is a special API endpoint /settings that I get the values from. 为此,有一个特殊的API端点/settings ,可以从中获取值。

The whole app is rendered on the server side when the user reloads the page. 当用户重新加载页面时,整个应用程序将在服务器端呈现。 I fire the settings fetch as soon as possible (in the componentWillMount of the top level component), but there is sometimes a blink/ delay. 我火的设置,尽快取(在componentWillMount顶级组件),但有时一个眨眼/延迟。

Possible solutions: 可能的解决方案:

  1. First solution is to fetch the settings when the user loads the app for the first time, and save it in the localStorage if it's available. 第一种解决方案是在用户首次加载应用程序时获取设置,并将其保存在localStorage如果有)。 Downside is that the numbers/ currencies still can be different than those in the settings when the app is loaded for the first time. 缺点是,首次加载应用程序时,数字/货币仍可能与设置中的数字/货币不同。

  2. Second solution would be to fetch the data before the application is rendered on the server side, and inject this data somewhere into script tag (like a window.userSettings = { ...settings } ). 第二种解决方案是在服务器上呈现应用程序之前获取数据,并将此数据注入脚本标记中的某个位置(例如window.userSettings = { ...settings } )。 It might extend the reload loading time a bit, but the settings will be as the user set them. 它可能会稍微延长重新加载的加载时间,但是设置将根据用户的设置进行。

Is there any other solution to such a problem? 还有其他解决方案吗? What is the best way to do it? 最好的方法是什么?

I hope, this solution may help you. 希望该解决方案可以为您提供帮助。

step 1 : Perform API call in componentWillMount and also, check for error. 步骤1 :在componentWillMount执行API调用,然后检查错误。 Assign two state one for currencies and one for error , if possible another one for loading Step 2 : Assign localStorage using your state in componnetDidMount Step 3 : Then, under your render method, check for both error state and localStorage 分配两个国家一个是currencies ,一个用于error ,如果可能,另一个用于loading 第2步 :指定localStorage使用状态componnetDidMount 第3步 :然后,你的渲染方法下,检查这两个error statelocalStorage

Sample snippet in given below 样本片段如下

class App extends Component{

  custructor(props){
    super(props);
    this.state={
      currency_number: '',
      error: false
    }
  }

  componentWillMount(){
    //perform fetch api and assign the state with the received items
    // check for error, if so then setState({error: true})
  }

  ComponentDidMount(){
    localStorage.setItem('currency', JSON.stringify(this.state.currency_number))
  }

  render(){
    const currency =  JSON.parse(localStorage.getItem('currency'))
    if (this.state.error) {
      return(
        <div>
          { currency }
        </div>

      )
    }
  }
}

I don't know if this can help you but you can render something else while the data is being fetched to make the user wait when there is a delay or to avoid the blinking effect. 我不知道这是否对您有帮助,但是您可以在获取数据时渲染其他内容,以使用户在有延迟的情况下等待或避免闪烁效果。 I am using loaders that I have found there 我使用,我发现装载机

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

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