当前位置:网站首页>12. Installation and use of mobx 6, enable decorator syntax
12. Installation and use of mobx 6, enable decorator syntax
2022-07-17 23:56:00 【Yamakawa Pro】
- Download related packages
npm install mobx mobx-react --save
npm install -D
@babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties
@babel/plugin-proposal-private-methods @babel/plugin-proposal-private-property-in-object
- To configure babel file (.babelrc)
"plugins": [
...
["@babel/plugin-proposal-decorators", {
"legacy": true }],
["@babel/plugin-proposal-class-properties", {
"loose": true }],
["@babel/plugin-proposal-private-methods", {
"loose": true }],
["@babel/plugin-proposal-private-property-in-object", {
"loose": true }],
...
]
// If it is ts project , Need to be in tsconfig.json Add this sentence to
{
"compilerOptions": {
...
"experimentalDecorators": true,
...
}
}
- newly build store Folder , And create index.tsx Master file and related store class , The relevant code is as follows
● index.tsx
import ListStore from './ListStore';
const stores = {
ListStore,
};
export {
stores };
export default stores;
● Create a new one ListStore Test class
import {
action, makeObservable, observable, computed } from 'mobx';
class ListStore {
@observable count: number = 0;
@observable selectUserList: Array<{
name: string; key: string }> = [];
@action.bound
add() {
this.count += 1;
}
@action.bound
sub() {
this.count -= 1;
}
@computed
get countComputedVal() {
return this.count * 2;
}
@action.bound
handleSelectUserList(userList: Array<{
name: string; key: string }>) {
this.selectUserList = userList;
}
constructor() {
makeObservable(this);
}
}
export default new ListStore();
- Import index.tsx Main input file , And configuration
import React from 'react';
import {
createRoot } from 'react-dom/client';
import {
Provider } from 'mobx-react';
import {
ConfigProvider } from 'antd';
import zhCN from 'antd/lib/locale/zh_CN';
import moment from 'moment';
import App from './app';
import 'moment/locale/zh-cn';
moment.locale('en');
import stores from '@/store';
import 'antd/dist/antd.less';
import './global.less';
const root = createRoot(document.getElementById('root'));
root.render(
<Provider {
...stores}>
<ConfigProvider
locale={
zhCN}
getPopupContainer={
(node) => document.body}
>
<App />
</ConfigProvider>
</Provider>,
);
5. Used on the page mobx The state of , The code is as follows :
import {
inject, observer } from 'mobx-react';
import React from 'react';
import {
Button } from 'antd';
function Home(props) {
const {
ListStore } = props;
const {
count, add, sub, selectUserList } = ListStore;
return (
<div className="btn-div">
<div> The number :{
count}</div>
<Button onClick={
add}>+</Button>
<Button onClick={
sub}>-</Button>
<div>
Selected personnel :{
selectUserList.length > 0 ? selectUserList.map((v) => v.name).join('、') : ' There is no selected person '}
</div>
</div>
);
}
export default inject('ListStore')(observer(Home));
边栏推荐
- 07 BTC mining development related history
- pytest+request+allure+excel接口自动化搭建 从0到1【二 读取Excel Case信息】
- 最牛逼的集群监控系统,它始终位列第一!
- Implementation of wechat applet Netease cloud music cloud music
- Problems and solutions of wechat applet related knowledge points and cloud music project production
- leecode17电话号码的字母组合
- Apply for SSL certificate with IP
- hyper子查询优雅实现join查询
- PolarDB for PostgreSQL结合Ceph共享存储的结构图是怎样的?
- 内核的结构和设计
猜你喜欢

Byte encountered a wave of resignation!

美团一面:@Transactional 原理和常见的坑?

统计年龄分布情况(5岁的间隔统计),绘制出年龄分布图。

hyper子查询优雅实现join查询

. Net webapi to realize interface version control and get through swagger support

Locust Performance Test 1 (Understanding)

今天去 OPPO 面试,被问麻了。。。

leecode17电话号码的字母组合

Introduction to Kali system use of hping3

API interface of all products in JD store (JD whole store product query API interface)
随机推荐
【Unity-学习-023】对象池Pro
Enterprise naming conventions
API interfaces for all goods in JD store (item_search_shop- API interfaces for all goods in the store), API interfaces for goods in the whole store
PolarDB for PostgreSQL结合Ceph共享存储的结构图是怎样的?
Degree engine (XI): Audio loading
Is decentralized science the new trend of Web 3.0?
淘宝官方商品、交易、订单、物流接口列表(淘宝oAuth2.0接口)
[advanced C language] - common string functions
使用 docker 搭建 grafana+prometheus 监控数据库资源(贰)
Go language pointer
PLC approximately calculates the water tank flow (FC) through Bernoulli equation
Today, I went to oppo for an interview and got numb...
Locust performance test 1 (understanding)
The most common algorithm interview questions
最牛逼的集群监控系统,它始终位列第一!
【C语言入门】----指针(初阶)
Quick connect product details API interface (item_get get get aliexpress product details interface)
hyper子查询优雅实现join查询
Bool type and related operators
今天去 OPPO 面试,被问麻了。。。