简体   繁体   English

componentDidMount可以在页面之间共享数据吗?

[英]Can componentDidMount share data between pages?

I am still learning React and I apologize if this is a stupid question. 我仍在学习React,如果这是一个愚蠢的问题,我深表歉意。 I am currently planning about the architecture of my simple App. 我目前正在计划我的简单App的体系结构。

I am using Next.js for this project 我正在为此项目使用Next.js

I have a component that calls the third party API for data every 5 seconds. 我有一个组件,每5秒调用一次第三方API的数据。 The data is shared among all pages in the app. 数据在应用程序的所有页面之间共享。

If the component that is fetching the data is not on the main/home page. 如果正在获取数据的组件不在主页/主页上。 Are there anyways for homepage to get the data it needs from another page? 无论如何, homepage是否可以从另一个页面获取所需的数据?

For example [Below are all pages] 例如[下面是所有页面]

index.js // Plain simple page that displays current weather and top music
http://localhost:3000/

weatherforecast.js //Using componentDidMount every 5 seconds to fetch Weather Data
http://localhost:3000/weatherforecast

musicplaylist.js //Using componentDidMount every 5 seconds to fetch Weather
http://localhost:3000/musicplaylist

The data shown in homepage has to be refreshed every 5 seconds if there are changes to the following data in weatherforecast.js and musicplaylist.js 如果weatherforecast.jsmusicplaylist.js的以下数据发生变化,则首页中显示的数据必须每5秒刷新一次

I had this in mind but I have a feeling that it's not the right way to do it. 我已经想到了这一点,但是我感觉这不是正确的方法。

In the Homepage.js , include WeatherForecast and MusicPlaylist components to fetch the data. Homepage.js ,包括WeatherForecastMusicPlaylist组件以获取数据。 If this is the case, it seems like I am repeating the same principle in every page. 如果是这种情况,似乎我在每一页中都重复了相同的原则。

I found out about Redux which store states as a global object. 我发现了有关Redux的信息 ,该信息将状态存储为全局对象。 But how does the state know when to update. 但是状态如何知道何时更新。 But before we getting to state, I am still not sure if components on another page can fetch the data without the user accessing the page. 但是在开始陈述之前,我仍然不确定另一页上的组件是否可以在用户不访问该页的情况下获取数据。

Thanks for reading this question. 感谢您阅读此问题。

Regarding the state change notification, refer to below image, and hopefully it is worth one thousand of words: 关于状态更改通知,请参考下图,希望它值一千个字:

在此处输入图片说明

Original article here . 原始文章在这里


Redux通过prop在连接的所有组件之间传递数据,因为只要接收到新的prop就会安装一个组件,那么就会启动一个新的渲染周期,以反映您的更改,此外,如果您不想使用Redux,可以尝试使用新上下文API,它很容易实现,也将是适合您的解决方案在这里是如何使用它的教程

For a pure React solution, you need to utilize other lifecycle methods available for React. 对于纯React解决方案,您需要利用React可用的其他生命周期方法。 componentDidMount is a good spot for making API requests, so you're in the right place to start. componentDidMount是发出API请求的好地方,因此您来对地方了。 However, componentDidMount occurs only once, right after the componentWillMount and after the DOM is ready with a complete render of the component. 但是, componentDidMount仅发生一次,就在componentWillMount之后,并且在DOM准备好组件的完整呈现之后。 Setting your API call to run at a set interval will not trigger a rerender of the component, no any of it's sub components. 将您的API调用设置为按设定的时间间隔运行不会触发该组件的重新呈现,也不会触发任何子组件。 Instead, use componentWillRecieveProps to add your interval request logic. 而是使用componentWillRecieveProps添加间隔请求逻辑。 After each interval completes, run setState with the new data from the request to update the default state defined in your constructor. 每个时间间隔完成后,使用请求中的新数据运行setState以更新构造函数中定义的默认状态。 As a bonus step to improve performace, follow up with function that returns a bool in componentShouldUpdate . 作为提高性能的一个额外步骤,请跟踪在componentShouldUpdate中返回布尔值的函数。 This way you can strictly define how and when and what is causing any and all component re-rendering. 这样,您可以严格定义导致任何和所有组件重新渲染的方式,时间和原因。

Redux is an excellent solution to take care of what you want to do. Redux是照顾您要做什么的绝佳解决方案。 Personally always use it in my React projects to manage state. 个人总是在我的React项目中使用它来管理状态。 Using Redux, you could make your API calls still occur in componentDidMount , however the call can hook into your Redux store and update your initial state. 使用Redux,您可以使API调用仍然在componentDidMount发生,但是该调用可以挂接到Redux存储中并更新您的初始状态。 The frees you up from have to worry about the local state of your component and how to go about conditionally rendering everything else in your app. 使您从中解放出来的方法不必担心组件的本地状态以及如何有条件地呈现应用程序中的其他所有内容。 Plus Redux abstracts other functions you need, like re-running your fetchToAPI in certain intervals, into their own source (known as Action Creators). 另外,Redux将您需要的其他功能抽象为它们自己的源(称为Action Creators),例如以一定的间隔重新运行fetchToAPI。 Action Creators hook directly in the Redux store so that when one is used, the resulting state diff is passed to Redux's Reducers, which in turn update the application global state. Action Creators直接挂接到Redux存储中,以便在使用Redux存储时,将结果状态差异传递给Redux的Reducers,后者进而更新应用程序的全局状态。 Then all that needs to be done is to have all of your components that need re-rendering on global state change to listen for state changes that occur in the Redux store and conditionally re-render based on the diff of the store from prevState => newProps. 然后,所有需要做的就是让您的所有组件都需要在全局状态更改时重新呈现,以侦听Redux存储中发生的状态更改,并根据prevState =>中存储的差异有条件地重新呈现newProps。 This can be setup fairly easy using the boolean check in componentShouldUpdate . 使用componentShouldUpdate的布尔检查可以很容易地设置它。

Hope this helps! 希望这可以帮助! Cheers. 干杯。

To pass data between pages in next.js you will have to use Redux. 要在next.js页面之间传递数据,您将不得不使用Redux。 The idea of Redux is to have a single source of truth. Redux的想法是拥有单一的真理来源。 In redux you update the state by calling actions . 在redux中,您可以通过调用actions更新状态。 To update redux state you have to create something called action creators which dispatches action to update the state. 要更新redux状态,您必须创建一个称为action creators东西,该action会分派action来更新状态。

Answer to your question as to if components on another page can fetch data without the user accessing the page is that they do not need to access the data. 关于另一个页面上的组件是否可以在无需用户访问页面的情况下获取数据的问题的答案是,它们不需要访问数据。 The components are mounted only when they are accessed, so it will fetch the data when they are mounted . 该组件仅安装他们被访问时,使它们被安装时,将取回的数据。

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

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