阿里前端推出的 React 框架Mirror

阿里前端推出的 React 框架Mirror

感觉更像是一个脚手架,省去了一些重复劳动,提高开发效率。

基于 React、Redux 和 react-router

写法:

import mirror, { actions } from 'mirrorx'
let nextId = 0
mirror.model({
name: 'todos',
initialState: [],
reducers: {
add(state, text) {
return [...state, {text, id: nextId++}]
},
complete(state, id) {
return state.map(todo => {
if (todo.id === id) todo.completed = true
return todo
})
}
}
})
// ...
// 在某个事件处理函数中
actions.todos.add('a new todo')
// 在另一个事件处理函数中
actions.todos.complete(42)

异步 action

mirror.model({
// 省略前述代码
effects: {
async addAsync(data, getState) {
const res = await Promise.resolve(data)
// 调用 `actions` 上的方法 dispatch 一个同步 action
actions.todos.add(res)
}
}
})

安装:

使用 create-react-app 创建一个新的 app:

$ npm i -g create-react-app
$ create-react-app my-app

创建之后,从 npm 安装 Mirror:

$ cd my-app
$ npm i --save mirrorx
$ npm start


分享到:


相關文章: