Different events in multiple components of React produce the same action

as mentioned, I have a project with multiple components, all of which have some event handling and operations. And different action events within these components may result in the same result, including updates to the redux.

for example, I have two methods that operate on redux, asce,update, listens on one of the buttons in component A, and triggers asce (), update () when pressed. Also listen to one of the buttons in component B and trigger both methods when pressed.
doesn"t seem to be a problem at the moment, because the method is relatively simple, but if I need to do some processing and complex operations on the data before or when there are more components, it seems very complex to write, resulting in the same complicated code in multiple places.

is there any way to make code with the same function in different components be written only once?

Mar.04,2021

the same code in different components, you can extract it to make a common component.
then import in different components.


these operations should be encapsulated in redux and placed in action creator or middleware. Components other than container should be completely unaware of the existence of redux and are only responsible for calling props.

Menu