您好,登錄后才能下訂單哦!
在AntDesign項目中集成Redux或MobX,可以按照以下步驟進行:
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));
通過以上步驟,在AntDesign項目中成功集成Redux或MobX并使用其管理應用的狀態。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。