C
C
chelkaz2018-11-15 17:04:30
React
chelkaz, 2018-11-15 17:04:30

Is it possible to just store shared data between components in React and Redux?

The application has a lot of independent components. How can you record and track data from anywhere?
Now we have to write all the variables to mapStateToProps in each component. But the variables are growing.
An example is this:
For example, a separate component list of cities on the side of the site.
By clicking on the city, an action is performed and through dispatch, the API data is loaded into the Redux store, but after that, you need to hide the other 5 different components, and show one with information about this city. And you need to get the information from the store.
What is the correct logic for working with Redux, so that there is no confusion. For the most part, I need to quickly write and pull data from shared storage on clicks and other events.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexhovansky, 2018-11-15
@alexhovansky

You have a component, let's call it A1.
There is a component with a list of cities, let's call it Cities.
Here you did something in Cities, action was called in the results. This action has a type, let's say it's CITY_SELECT.
In a reducer that transforms your store after a successful (or not so successful) request, you should have something like if (action.type === CITY_SELECT_SUCCESS) { return someObject }
This someObject besides action.payload. data pass some boolin like hideComponentA1: true. And hide component A1 when this hideComponentA1 is received.
Probably a very obvious answer, I would do just that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question