Files
freeCodeCamp/curriculum/challenges/chinese-traditional/03-front-end-libraries/redux/create-a-redux-store.md
Nicholas Carrigan (he/him) 3da4be21bb chore: seed chinese traditional (#42005)
Seeds the chinese traditional files manually so we can deploy to
staging.
2021-05-05 22:43:49 +05:30

2.1 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5a24c314108439a4d403614b 創建一個 Redux Store 6 301439 create-a-redux-store

--description--

Redux 是一個狀態管理框架,可以與包括 React 在內的許多不同的 Web 技術一起使用。

在 Redux 中,有一個狀態對象負責應用程序的整個狀態, 這意味着如果你有一個包含十個組件且每個組件都有自己的本地狀態的 React 項目,那麼這個項目的整個狀態將通過 Redux store 被定義爲單個狀態對象, 這是學習 Redux 時要理解的第一個重要原則Redux store 是應用程序狀態的唯一真實來源。

這也意味着,如果應用程序想要更新狀態,只能通過 Redux store 執行, 單向數據流可以更輕鬆地對應用程序中的狀態進行監測管理。

--instructions--

Redux store 是一個保存和管理應用程序狀態的state 可以使用 Redux 對象中的 createStore() 來創建一個 redux store 此方法將 reducer 函數作爲必需參數, reducer 函數將在後面的挑戰中介紹。該函數已在代碼編輯器中爲你定義, 它只需將 state 作爲參數並返回一個 state 即可。

聲明一個 store 變量並把它分配給 createStore() 方法,然後把 reducer 作爲一個參數傳入即可。

注意: 編輯器中的代碼使用 ES6 默認參數語法將 state 的值初始化爲 5 如果你不熟悉默認參數,你可以參考ES6 全部課程,它裏面涵蓋了這個內容。

--hints--

Redux store 應當存在。

assert(typeof store.getState === 'function');

Redux store 的 state 的值應該爲 5。

assert(store.getState() === 5);

--seed--

--seed-contents--

const reducer = (state = 5) => {
  return state;
}

// Redux methods are available from a Redux object
// For example: Redux.createStore()
// Define the store here:

--solutions--

const reducer = (state = 5) => {
  return state;
}

const store = Redux.createStore(reducer);