Context API
Context API
React 16.3後新增了Context API,可以用來替代Redux。
官方文件:
不錯的文章:
https://medium.freecodecamp.org/replacing-redux-with-the-new-react-context-api-8f5d01a00e8c
可參考此範例:
https://github.com/EasonWang01/React-context-api-boilerplate
範例:
App.js
Login.js
步驟:
1.使用Provider提供根組件,任何包在Provider內的子組件均可使用Consumer來存取根組件數據。
2.在react-router把consumer傳給元件。
3.在元件內使用Consumer包住所有dom,dom內即可存取provider的數據。
相關框架
https://github.com/didierfranc/react-waterfall
https://github.com/elisherer/react-waterfall-redux-devtools-middleware
Last updated
Was this helpful?