react+mobx 編寫 withStoreHistory 裝飾器
阿新 • • 發佈:2018-12-09
主要作用是向store裡面注入一個history物件,方便story裡面的函式呼叫
function withStoreHistory(storeName) { if (!storeName) return console.error(`必須輸入一個查詢資料的store`); return function(Target) { class WithStoreHistory extends Component { componentDidMount() { const { history } = this.props; const store = this.props[storeName]; store.history = history; } render() { return <Target {...this.props} />; } } return WithStoreHistory; }; }
使用
需要在inject呼叫後才能獲取到store的資料,所以寫在inject下面
const MERCHANTSTORE = "merchantStore";
@inject(MERCHANTSTORE)
@withStoreHistory(MERCHANTSTORE)
@observer
class BusinessEntrance extends Component {
render() {
return (
<div>...</div>
);
}
}
函式中使用
@action.bound handleSettingData() { this.history.push("/merchants_settled"); }