A
A
Artyom2020-11-11 18:23:54
React
Artyom, 2020-11-11 18:23:54

Where to fetch data that is used in multiple REDUX components?

I have the same data fetched on all pages.
Where can I fetch them so that I don't have to do it every time in every component?
The problem is that using the Router, we connect the components in this way.

<Layout>
      <Switch>          
          <Route path='/' exact component={EnrolleList}/> 
          <Route path='/create-enrolle' exact component={CreateEnrolle}/>
          <Route path='/schedule' exact component={Schedule}/>
          <Route path='/exams' exact component={Exams}/>
          <Route path='/results' exact component={Results}/>
          <Route path='/enrollee/:id' exact component={Enrollee}/> 
          <Route path='/faculty-list' exact component={FacultyList}/>  
          <Route path='/faculties/' exact component={Faculties}/> 
          <Route path='/faculty/:id' exact component={Facuclty}/> 
          <Route path='/edit-faculty' exact component={EditFaculty}/>         
          <Route path='/add-new-faculty' exact component={AddNewFaculty}/>       
          <Redirect to='/' />
        </Switch>
    </Layout>

In general, there would be no route, I would create a HOC and most likely fetch it there, after which I connected it to the APP.
But using routes, each component must be written separately for each route.
In general, please tell me where is the best place to do this, and where do you usually do it

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2020-11-11
@danyvasnafig

And to register in a component logic? If the data is already there, do not fetch, you can still register the date of the expair in the store, they say if the fetch was 10 minutes ago, perefetch.
The same code, it is not necessary to write in each component.

V
Vladimir, 2020-11-11
@HistoryART

When you start the application => app.js and write data to redux already there, you will get one request to the server (Unless of course you want to update them. If you need to update, create a call in redux that will inject into useEffect like buddy, update data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question