2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 5a24c314108439a4d403614c
|
|
|
|
title: Get State from the Redux Store
|
|
|
|
challengeType: 6
|
|
|
|
isRequired: false
|
2020-09-07 16:16:17 +08:00
|
|
|
forumTopicId: 301443
|
|
|
|
localeTitle: 从 Redux Store 获取状态
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-09-07 16:16:17 +08:00
|
|
|
<section id='description'>
|
|
|
|
Redux store 对象提供了几种允许你与之交互的方法,你可以使用<code>getState()</code>方法检索 Redux store 对象中保存的当前的<code>state</code>。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-09-07 16:16:17 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
在代码编辑器中可以将上一个挑战中的代码更简洁地重写,在<code>store</code>中使用<code>store.getState()</code>检索<code>state</code>,并将其分配给新变量<code>currentState</code>。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-09-07 16:16:17 +08:00
|
|
|
- text: redux store 的 state 应该有一个初始值 5。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(store.getState()===5);
|
2020-09-07 16:16:17 +08:00
|
|
|
- text: 应该存在一个变量<code>currentState</code>,并为其分配 Redux store 的当前状态。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: getUserInput => assert(currentState === 5 && getUserInput('index').includes('store.getState()'));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='jsx-seed'>
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
const store = Redux.createStore(
|
|
|
|
(state = 5) => state
|
|
|
|
);
|
|
|
|
|
2020-09-07 16:16:17 +08:00
|
|
|
// 更改此行下方的代码
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-09-07 16:16:17 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-09-07 16:16:17 +08:00
|
|
|
const store = Redux.createStore(
|
|
|
|
(state = 5) => state
|
|
|
|
);
|
|
|
|
|
|
|
|
// 更改此行下方的代码
|
|
|
|
const currentState = store.getState();
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2020-09-07 16:16:17 +08:00
|
|
|
</section>
|