To trigger a Redux action from outside a component with React, we call store.dispatch
from anywhere in our project.
For instance, we write
import { store } from "/path/to/createdStore";
function testAction(text) {
return {
type: "TEST_ACTION",
text,
};
}
store.dispatch(testAction("StackOverflow"));
to call the testAction
function to return the action type
and text
value.
And then we call store.dispatch
to with the object returned by testAction
to dispatch the TEST_ACTION
action.