定义和导出Store
import { action, observable } from "mobx";
//定义一个Store:User
class User {
name = 'LL';
sex = 'man';
@action getName = (val) => {
return this.name;//
}
@action setName = (val) => {
this.name = val;
}
}
export default new User();
//导出Store
import User from './user';
const store = {
User,
}
export default store;
将Store注入函数组件
import React, {useState, useEffect} from 'react';
import {inject } from "mobx-react";
export default inject('User')((props) => {
//使用Mobx
console.log('---', props.User)
return (
<div>
{props.User.getName()}
</div>
)
})
可以直接使用Inject方法注入到函数组件里面,即可正常使用mobx