您好,登錄后才能下訂單哦!
在AntDesign項(xiàng)目中集成Redux或MobX,可以按照以下步驟進(jìn)行:
npm install redux react-redux
npm install mobx mobx-react
import { createStore } from 'redux';
import rootReducer from './reducers';
const store = createStore(rootReducer);
export default store;
import { observable, action } from 'mobx';
class Store {
@observable count = 0;
@action increment() {
this.count++;
}
@action decrement() {
this.count--;
}
}
const store = new Store();
export default store;
import React from 'react';
import { Provider } from 'react-redux';
import store from './store';
import App from './App';
const Root = () => (
<Provider store={store}>
<App />
</Provider>
);
export default Root;
import React from 'react';
import { Provider } from 'mobx-react';
import store from './store';
import App from './App';
const Root = () => (
<Provider store={store}>
<App />
</Provider>
);
export default Root;
import React from 'react';
import { connect } from 'react-redux';
const Counter = ({ count, increment, decrement }) => (
<div>
<h1>{count}</h1>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
const mapStateToProps = state => ({
count: state.count,
});
const mapDispatchToProps = {
increment: () => ({ type: 'INCREMENT' }),
decrement: () => ({ type: 'DECREMENT' }),
};
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
import React from 'react';
import { inject, observer } from 'mobx-react';
const Counter = ({ store }) => (
<div>
<h1>{store.count}</h1>
<button onClick={store.increment}>Increment</button>
<button onClick={store.decrement}>Decrement</button>
</div>
);
export default inject('store')(observer(Counter));
通過(guò)以上步驟,在AntDesign項(xiàng)目中成功集成Redux或MobX并使用其管理應(yīng)用的狀態(tài)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。