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的更多相关文章

  1. [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 ...

  2. React使用Mobx管理数据

    React 和 Vue一样都属于单向数据流,为了更好的进行状态和数据管理官方和第三方也有配套的Redux等插件,本文介绍一个个人觉得更易用使用的组件 Mobx 核心概念 MobX 处理你的应用程序状态 ...

  3. react+react-router+mobx+element打造管理后台系统---react-amdin-element

    react-admin-element,一款基于react的后台管理系统. 那么我们和其他的后台管理系统有什么区别呢? demo地址:点我进入demo演示 github地址:点我进入github 1. ...

  4. React + MobX 状态管理入门及实例

    前言 现在最热门的前端框架,毫无疑问是React. React是一个状态机,由开始的初始状态,通过与用户的互动,导致状态变化,从而重新渲染UI. 对于小型应用,引入状态管理库是"奢侈的&qu ...

  5. 从零配置webpack(react+less+typescript+mobx)

    本文目标 从零搭建出一套支持react+less+typescript+mobx的webpack配置 最简化webpack配置 首页要初始化yarn和安装webpack的依赖 yarn init -y ...

  6. mobx在react的使用

    ​ 创建项目第六步 mobx 1.安装 yarn add mobx yarn add mobx-react 2.新建/src/store/store.js import {observable, co ...

  7. React MobX 开始

    MobX 用于状态管理,简单高效.本文将于 React 上介绍如何开始,包括了: 了解 MobX 概念 从零准备 React 应用 MobX React.FC 写法 MobX React.Compon ...

  8. mobx @computed的解读

    写在前面:我一开始看不懂官网的@computed的作用,因为即使我把@computed去掉,依然能正确的report,然后我百度谷歌都找不到答案,下面都是我自己的理解,如果是有问题的,不对的,请务必留 ...

  9. React服务端渲染总结

    欢迎吐槽 : ) 本demo地址( 前端库React+mobx+ReactRouter ):https://github.com/Penggggg/react-ssr.本文为笔者自学总结,有错误的地方 ...

随机推荐

  1. 配置gitlab通过smtp发送邮件

    1. 编辑/etc/gitlab/gitlab.rb文件(加到文件最后面就好了,或者通过搜索找到,新版已有这些配置,只不过都是被注释掉了).以QQ企业邮箱为例: gitlab_rails['smtp_ ...

  2. mysql调优最大连接数

    一.mysql调优 1.1 报错: Mysql: error 1040: Too many connections 1.2 原因: 1.访问量过高,MySQL服务器抗不住,这个时候就要考虑增加从服务器 ...

  3. jvm理论-class文件

    当JVM运行Java程序的时候,它会加载对应的class文件,并提取class文件中的信息存放在JVM的方法区内存中. Class文件组成 1.Class文件是一组以8位字节为基础单位的二进制流,各个 ...

  4. 【理论面试篇】收集整理来自网络上的一些常见的 经典前端、H5面试题 Web前端开发面试题

    ##2017.10.30收集 面试技巧 5.1 面试形式 1)        一般而言,小公司做笔试题:大公司面谈项目经验:做地图的一定考算法 2)        面试官喜欢什么样的人 ü  技术好. ...

  5. 让rpc支持双向通信

    rpc采用了C/S模型,不支持双向通信:client只能远程调用server端的RPC接口,但client端则没有RPC供server端调用,这意味着,client端能够主动与server端通信,但s ...

  6. SSM 整合 quartz JDBC方式实现job动态增删改查记录

    虽然网上有很多资料,但是都不够系统,本文记录下自己的整合过程. 1. 搭建一个SSM项目,此处略. 2. 按照quartz官方要求,建立quartz相关的数据库和表,相关sql语句如下: /* Nav ...

  7. What are some good books/papers for learning deep learning?

    What's the most effective way to get started with deep learning?       29 Answers     Yoshua Bengio, ...

  8. H3C Comware V3 端口聚合

    通常链路聚合有三种模式:手工汇聚.静态LACP汇聚和动态LACP汇聚. 但是V3版本下只提供了 手工聚合模式 manual 和 静态LACP聚合模式 static 两种 V3版本配置链路聚合 1,创建 ...

  9. Canvas入门到高级详解(上)

    神奇的 canvas--AICODER 全栈培训 IT 培训专家 一.canvas 简介 1.1 什么是 canvas?(了解) 是 HTML5 提供的一种新标签 <canvas>< ...

  10. Windows 10 Manager v2.3.3

    Windows 10 Manager 是专门用于微软 Windows10 的集所有功能于一身的实用工具,它包括了40多个不同的实用程序来优化.调整.清理.加快和修复您的 Windows 10,可以让你 ...