dispatch
方法是您用于将操作分派给Redux存储的方法。调用store.dispatch()
并传递从操作创建者返回的值会将操作发送回商店。回想一下,动作创建者返回一个具有type属性的对象,该属性指定已发生的动作。然后,该方法将操作对象调度到Redux存储。根据之前的挑战示例,以下行是等效的,并且都调度LOGIN
类型的操作: store.dispatch(actionCreator());
store.dispatch({type:'LOGIN'});
false
的login
属性的对象。还有一个名为loginAction()
的动作创建器,它返回LOGIN
类型的动作。通过调用dispatch
方法将LOGIN
操作发送到Redux存储,并传入loginAction()
创建的操作。 loginAction
应该返回一个对象,其type
属性设置为字符串LOGIN
。
testString: 'assert(loginAction().type === "LOGIN", "Calling the function loginAction
should return an object with type
property set to the string LOGIN
.");'
- text: 应使用属性login
设置为false
的对象初始化存储。
testString: 'assert(store.getState().login === false, "The store should be initialized with an object with property login
set to false
.");'
- text: store.dispatch()
方法应该用于调度LOGIN
类型的操作。
testString: 'getUserInput => assert((function() { let noWhiteSpace = getUserInput("index").replace(/\s/g,""); return noWhiteSpace.includes("store.dispatch(loginAction())") || noWhiteSpace.includes("store.dispatch({type: \"LOGIN\"})") === true })(), "The store.dispatch()
method should be used to dispatch an action of type LOGIN
.");'
```