The main idea of connect
with mapDispatchToProps
and mapStateToProps
is to keep you UI components simple and easily reusable. Imagine, if you have 3 applications which have completely different architecture(redux, flux, pure React ContextAPI), but should reuse same UI components. If you incapsulate business logic directly to your components (2. example), then it might be very hard or even impossible to reuse it, because it is attached to some store
(in the place where you use your <Component ... />
).
Just as a side note and good example, how mapDispatchToProps
can make your application clean is clear separation between business logic and UI component.
Example:
You have a Button
component, which is used all over the application. Then you get a requirement that you need to have a logout button in different places of your application. One way would be to create a new regular React component, which will use the Button
component inside and also have some logic.
connect
with mapDispatchToProps
comes to help you and allow to easily create a new LogoutButton
component with attached logic (which also allows you to update the redux state), then whenever you need to have a logout button you simply use LogoutButton
and there will be no need in any extra logic because it's all defined inside mapDispatchToProps
and is already attached to the Button
component.