react使用mobx
mobx api 使用装饰器语法 mobx数据转化为js数据
安装
yarn add mobx mobx-react
yarn add babel-preset-mobx --dev
"presets": ["mobx"]
添加 /src/store/index.js
import {
observable,
action,
computed
} from 'mobx'
class Store {
@observable state = {
title: 'Header..'
}
@computed get get_header() {
return this.state.title + 'Ajanuw';
}
@action.bound set_title() {
if (Math.random() > 0.5) {
this.state.title += '.'
} else {
let title = this.state.title
if (title[title.length - 1] !== '.') return;
this.state.title = title.substring(title.length - 1, 0);
}
}
}
export default new Store();
/src/App.js
import React, { Component } from 'react';
import './App.css';
import { observer } from 'mobx-react'
import store from './store/index';
const Play = observer( prop =>{
return (
<div>
<div>{ store.get_header}</div>
<button onClick={ store.set_title }>change</button>
</div>
);
});
@observer
class App extends Component {
render() {
return (
<div className="App">
<Play />
</div>
);
}
}
export default App;
使用rxjs
@action.bound change(){
of(toJS(this.state.msg))
.pipe(
map(v => v += '..')
).subscribe(v => this.state.msg = v);
}
节流
<Button color='secondary' onClick={store.getJson('ajanuw')}>Get Json</Button>
@action.bound getJson = name => throttle( e => {
l(name)
}, 2000);
最佳实践 RootStore
// 插入到组件树
import { Provider, inject } from "mobx-react";
import RootStore from "./store/root.store";
<Provider {...new RootStore()}>
<App />
</Provider>
---
// 创建 RootStore
import IndexStore from "./index.store";
import WelcomeStore from "./welcome.store";
class RootStore {
constructor() {
this.indexStore = new IndexStore(this);
this.welcomeStore = new WelcomeStore(this);
}
}
export default RootStore;
---
// 单个 store
import { observable, action, computed } from "mobx";
const l = console.log;
class IndexStore {
constructor(rootStore) {
this.rootStore = rootStore;
}
@observable
name = "indexStore";
@action.bound
test() {
// 使用另一个store的数据
l(this.rootStore.welcomeStore.name);
}
}
export default IndexStore;
---
// 使用
@inject(({ indexStore }) => ({ indexStore }))
componentWillMount() {
this.props.indexStore.test()
}
关于 action
箭头函数不需要设置bound,传递参数的函数把action绑定在第二个函数上
@action.bound
asd() {
this.a = 1;
this.s = 2;
}
@action
asd = () => {
this.a = 1;
this.s = 2;
};
asd = k =>
action(() => {
this.a = 1;
this.s = 2;
});
在组件中传递 mobx的数据
MobX 追踪属性访问,而不是值
尽量使用上面的实例
const Title = observer(({ children }) => <h2>{children.title}</h2>);
<Title>{data}</Title>
const Title = observer(({ children }) => <h2>{children}</h2>);
<Title>{data.title}</Title>
异步方法最好使用 runInAction
@action.bound
async getList() {
let r = await getData();
runInAction(() => {
this.list = r;
});
}
react使用mobx的更多相关文章
- [Web] How to Test React and MobX with Jest
转载自: https://semaphoreci.com/community/tutorials/how-to-test-react-and-mobx-with-jest?utm_content=bu ...
- React使用Mobx管理数据
React 和 Vue一样都属于单向数据流,为了更好的进行状态和数据管理官方和第三方也有配套的Redux等插件,本文介绍一个个人觉得更易用使用的组件 Mobx 核心概念 MobX 处理你的应用程序状态 ...
- react+react-router+mobx+element打造管理后台系统---react-amdin-element
react-admin-element,一款基于react的后台管理系统. 那么我们和其他的后台管理系统有什么区别呢? demo地址:点我进入demo演示 github地址:点我进入github 1. ...
- React + MobX 状态管理入门及实例
前言 现在最热门的前端框架,毫无疑问是React. React是一个状态机,由开始的初始状态,通过与用户的互动,导致状态变化,从而重新渲染UI. 对于小型应用,引入状态管理库是"奢侈的&qu ...
- 从零配置webpack(react+less+typescript+mobx)
本文目标 从零搭建出一套支持react+less+typescript+mobx的webpack配置 最简化webpack配置 首页要初始化yarn和安装webpack的依赖 yarn init -y ...
- mobx在react的使用
创建项目第六步 mobx 1.安装 yarn add mobx yarn add mobx-react 2.新建/src/store/store.js import {observable, co ...
- React MobX 开始
MobX 用于状态管理,简单高效.本文将于 React 上介绍如何开始,包括了: 了解 MobX 概念 从零准备 React 应用 MobX React.FC 写法 MobX React.Compon ...
- mobx @computed的解读
写在前面:我一开始看不懂官网的@computed的作用,因为即使我把@computed去掉,依然能正确的report,然后我百度谷歌都找不到答案,下面都是我自己的理解,如果是有问题的,不对的,请务必留 ...
- React服务端渲染总结
欢迎吐槽 : ) 本demo地址( 前端库React+mobx+ReactRouter ):https://github.com/Penggggg/react-ssr.本文为笔者自学总结,有错误的地方 ...
随机推荐
- 咏南DELPHI7中间件+开发框架
咏南DELPHI7中间件+开发框架 演示下载:链接: https://pan.baidu.com/s/1bulGBIZ6A1nkeErxIrGsGA 密码: 22dk 解压后运行ynmain.exe ...
- 前端工程化系列[01]-Bower包管理工具的使用
本文主要介绍前端开发中常用的包管理工具Bower,具体包括Bower的基本情况.安装.使用和常见命令等内容,最后还介绍了依赖树管理的常见方式以及Bower采用的策略并进行了比较. 1.1 关于Bowe ...
- 修复android 5.0 Xutils的框架问题retry error, curr request is null
Android 5.0手机对xUtils-2.6.13.jar请求时会出现retry error, curr request is null 情况, 修复解决方式: 找到library/src/com ...
- c groups
https://www.kernel.org/doc/Documentation/cgroup-v1/cgroups.txt https://developer.ibm.com/hadoop/2017 ...
- np.corrcoef()方法计算数据皮尔逊积矩相关系数(Pearson's r)
上一篇通过公式自己写了一个计算两组数据的皮尔逊积矩相关系数(Pearson's r)的方法,但np已经提供了一个用于计算皮尔逊积矩相关系数(Pearson's r)的方法 np.corrcoef() ...
- 程序猿必备的10款超炫酷HTML5 Canvas插件
1.超炫酷HTML5 Canvas 3D旋转地球动画 这是一款基于HTML5 Canvas的3D地球模拟动画,动画以太空作为背景,地球在太空中旋转,同时我们也可以拖拽鼠标来从不同的角度观察地球.另外我 ...
- Python threading 多参数传递方法
今天开启线程传递参数的时候,出现了一个小问题,一直不能传递多个参数,如下 import threading thread1 = threading.Thread(target=fun, args=[1 ...
- 【javascript】九宫格抽奖组件设计
一些主要点 1. 转圈的顺序(顺时针或者逆时针): 2. 转圈的速率(从慢到快再到慢): 3. 位置的问题(下一次抽奖的起始位置是上一次抽奖的结束位置): 4. 转圈的圈数或者移动的次数. 基本原理 ...
- 解决wps/office 1-2自动转换1月2日,用样式解决此问题
添加样式: td{mso-number-format:"\@"; }
- 消息中间件系列二:RabbitMQ入门(基本概念、RabbitMQ的安装和运行)
一.基本概念 1. AMQP AMQP,即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议.支持不同语言和不同的产品 2. 生产者 ...