charleyw / Wechat Weapp Redux
微信小程序Redux绑定
Stars: ✭ 464
Programming Languages
javascript
184084 projects - #8 most used programming language
Projects that are alternatives of or similar to Wechat Weapp Redux
Sentry Miniapp
Sentry 小程序/小游戏 SDK:用于小程序/小游戏平台的 Sentry SDK(目前支持微信、字节跳动、支付宝、钉钉、QQ、百度小程序,微信、QQ 小游戏)
Stars: ✭ 269 (-42.03%)
Mutual labels: weapp
Ironinfoweapp
铜陵回归物资有限公司支持, 钢材信息小程序(基于uni-app), 后台账密: tour, tour520
Stars: ✭ 257 (-44.61%)
Mutual labels: weapp
Lemonjournal
A WeChat mini program demo based on Wafer2 framework - 微信小程序Demo:柠檬手帐 - 界面简洁的图片编辑应用,支持图片和文字的移动、旋转、缩放、保存编辑状态并生成预览图
Stars: ✭ 341 (-26.51%)
Mutual labels: weapp
Wxapp Boilerplate
使用 webpack, babel, scss 开发的微信/支付宝小程序项目脚手架
Stars: ✭ 367 (-20.91%)
Mutual labels: weapp
微信小程序Redux绑定
用于在微信小程序为页面绑定Redux Store。
PS: 代码是基于react-redux修改的
安装
-
clone或者下载代码库到本地:
git clone https://github.com/charleyw/wechat-weapp-redux
-
将
dist/wechat-weapp-redux.js
(或者拷贝minify的也可以)文件直接拷贝到小程序的工程中,例如(下面假设我们把第三方包都安装在libs目录下):cd wechat-weapp-redux cp -r dist/wechat-weapp-redux.js <小程序根目录>/libs
上面的命令将包拷贝到小程序的
libs
目录下
使用
-
将Redux Store绑定到App上。
const store = createStore(reducer) // redux store const WeAppRedux = require('./libs/wechat-weapp-redux/index.js'); const {Provider} = WeAppRedux;
Provider是用来把Redux的store绑定到App上。
App(Provider(store)({ onLaunch: function () { console.log("onLaunch") } }))
provider的实现只是简单的将store加到App这个global对象上,方便在页面中用getApp取出来
上面这段代码等同于:
App({ onLaunch: function() { console.log( "onLaunch" ) }, store: store })
-
在页面的定义上使用connect,绑定redux store到页面上。
const pageConfig = { data: { }, ... }
页面的定义
const mapStateToData = state => ({ todos: state.todos, visibilityFilter: state.visibilityFilter })
定义要映射哪些state到页面
const mapDispatchToPage = dispatch => ({ setVisibilityFilter: filter => dispatch(setVisibilityFilter(filter)), toggleTodo: id => dispatch(toggleTodo(id)), addTodo: text => dispatch(addTodo(text)), })
定义要映射哪些方法到页面
const nextPageConfig = connect(mapStateToData, mapDispatchToPage)(pageConfig)
使用connect将上述定义添加到pageConfig中。
Page(nextPageConfig);
注册小程序的页面
-
说明
完成上述两步之后,你就可以在
this.data
中访问你在mapStateToData
定义的数据了。mapDispatchToPage
定义的action会被映射到this
对象上。
Example
详细的使用例子可以参照: wechat-weapp-redux-todos
真机实测版请clone下面这个repo,用小程序开发工具开启预览:
git clone -b release https://github.com/charleyw/wechat-weapp-redux-todos.git
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].