freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/redux/dispatch-an-action-event.ch...

2.7 KiB
Raw Blame History

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d403614f Dispatch an Action Event 6 false 派遣行动事件

Description

dispatch方法是您用于将操作分派给Redux存储的方法。调用store.dispatch()并传递从操作创建者返回的值会将操作发送回商店。回想一下动作创建者返回一个具有type属性的对象该属性指定已发生的动作。然后该方法将操作对象调度到Redux存储。根据之前的挑战示例以下行是等效的并且都调度LOGIN类型的操作:
store.dispatchactionCreator;
store.dispatch{type'LOGIN'};

Instructions

代码编辑器中的Redux存储具有初始化状态该状态是包含当前设置为falselogin属性的对象。还有一个名为loginAction()的动作创建器,它返回LOGIN类型的动作。通过调用dispatch方法将LOGIN操作发送到Redux存储并传入loginAction()创建的操作。

Tests

tests:
  - text: 调用函数<code>loginAction</code>应该返回一个对象,其<code>type</code>属性设置为字符串<code>LOGIN</code> 。
    testString: 'assert(loginAction().type === "LOGIN", "Calling the function <code>loginAction</code> should return an object with <code>type</code> property set to the string <code>LOGIN</code>.");'
  - text: 应使用属性<code>login</code>设置为<code>false</code>的对象初始化存储。
    testString: 'assert(store.getState().login === false, "The store should be initialized with an object with property <code>login</code> set to <code>false</code>.");'
  - text: <code>store.dispatch()</code>方法应该用于调度<code>LOGIN</code>类型的操作。
    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 <code>store.dispatch()</code> method should be used to dispatch an action of type <code>LOGIN</code>.");'

Challenge Seed

const store = Redux.createStore(
  (state = {login: false}) => state
);

const loginAction = () => {
  return {
    type: 'LOGIN'
  }
};

// Dispatch the action here:

Solution

// solution required