Email - harun.bspt2014@gmail.com Phone - +8801717615827

BindActionCreators

One of the crucial thing in Redux is binding the action creators to dispatch. Without this binding firing an action creator will do nothing. Now mapDispatchToProps is not the only way of bind dispatch and action creators, there is another way called bindActionCreators.

import { bindActionCreators } from "redux";
import * as TodoActionCreators from "./TodoActionCreators";

let boundActionCreators = bindActionCreators(TodoActionCreators, dispatch);

Per Redux documentation – The only use case for bindActionCreators is when you want to pass some action creators down to a component that isn’t aware of Redux, and you don’t want to pass dispatch or the Redux store to it.

The bindActionCreators method enables you to dispatch actions from a component which is not connected to the store, as mapDispatchToPros in the connect method of react-redux. Its usage is quite rare as react-redux will do the job

Generally for most use cases, when you want to dispatch an action from your component, you should first connect it with the store and use the connect method of react-redux

The purpose of mapDispatchToProps and bindActionCreators is to wrap up action creator functions inside of new functions, so they automatically dispatch as soon as they’re called (and your component doesn’t even need to know about dispatch). That’s what your first example does.

From Redux documentation

Turns an object whose values are action creators, into an object with the same keys, but with every function wrapped into a dispatch call so they may be invoked directly. This is just a convenience method, as you can call store.dispatch(MyActionCreators.doSomething()) yourself just fine.

Dr. Harun
Dr. Harun

Dr. Md. Harun Ar Rashid, MPH, MD, PhD, is a highly respected medical specialist celebrated for his exceptional clinical expertise and unwavering commitment to patient care. With advanced qualifications including MPH, MD, and PhD, he integrates cutting-edge research with a compassionate approach to medicine, ensuring that every patient receives personalized and effective treatment. His extensive training and hands-on experience enable him to diagnose complex conditions accurately and develop innovative treatment strategies tailored to individual needs. In addition to his clinical practice, Dr. Harun Ar Rashid is dedicated to medical education and research, writing and inventory creative thinking, innovative idea, critical care managementing make in his community to outreach, often participating in initiatives that promote health awareness and advance medical knowledge. His career is a testament to the high standards represented by his credentials, and he continues to contribute significantly to his field, driving improvements in both patient outcomes and healthcare practices.

Translate »
Register New Account