Files
freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/redux/get-state-from-the-redux-store.chinese.md

1.4 KiB

id, title, challengeType, isRequired, forumTopicId, localeTitle
id title challengeType isRequired forumTopicId localeTitle
5a24c314108439a4d403614c Get State from the Redux Store 6 false 301443 从 Redux Store 获取状态

Description

Redux store 对象提供了几种允许你与之交互的方法,你可以使用getState()方法检索 Redux store 对象中保存的当前的state

Instructions

在代码编辑器中可以将上一个挑战中的代码更简洁地重写,在store中使用store.getState()检索state,并将其分配给新变量currentState

Tests

tests:
  - text: redux store 的 state 应该有一个初始值 5。
    testString: assert(store.getState()===5);
  - text: 应该存在一个变量<code>currentState</code>,并为其分配 Redux store 的当前状态。
    testString: getUserInput => assert(currentState === 5 && getUserInput('index').includes('store.getState()'));

Challenge Seed

const store = Redux.createStore(
  (state = 5) => state
);

// 更改此行下方的代码

Solution

const store = Redux.createStore(
  (state = 5) => state
);

// 更改此行下方的代码
const currentState = store.getState();