Redux
Redux is a predictable state container for JavaScript apps.
概念
1.使用connect讓最上層元件取得Provider中的store,再用props傳下去
2.使用store.getState簡單範例
<!DOCTYPE html>
<html>
<head>
<title>Redux basic example</title>
<script src="https://npmcdn.com/redux@latest/dist/redux.min.js"></script>
</head>
<body>
<div>
<p>
Clicked: <span id="value">0</span> times
<button id="increment">+</button>
<button id="decrement">-</button>
<button id="incrementIfOdd">Increment if odd</button>
<button id="incrementAsync">Increment async</button>
</p>
</div>
</body>
</html>使用React連結Redux
另一個稍為更詳細的範例
流程:
實做:
接著幫他加上toggle
接著加入三個選項,分別顯示all,active,completed
使用combined reduecer
中間件 BindActionCreator
mapDispatchToProp
操作非同步動作(Async)
使用Redux-thunk
React-router-redux
記得加條件限制
Redux Devtools
stateless function components
(可將class改為const)
官方推薦使用
從store 給到元件
Last updated