1.同步
需求是:点击进入详情页面(Detail),隐藏选项卡,选项卡组件在App组件,需要在App组件中操作
入口文件index.js将model打开,并在models创建文件夹maizuo 在model中写命名空间,状态以及reducers
export default {
namespace: "maizuo",
state: {
isShow: true,
},
reducers: {
hide(prevState, action) {
return { ...prevState, isShow: false };
},
show(prevState, action) {
return { ...prevState, isShow: true };
},
},
};
App.js
import { connect } from 'dva'
import React, { Component } from 'react'
import Tabbar from "../components/Tabbar"
class App extends Component {
render() {
return (
<div>
App
{}
{this.props.children}
{}
{this.props.isShow && <Tabbar />}
</div>
);
}
}
export default connect((state) => {
return {
a:1,
isShow:state.maizuo.isShow
}
})(App);
Detail组件中发dispatch改变isShow的值
import { connect } from 'dva';
import React, { Component } from 'react'
class Detail extends Component {
componentDidMount(){
console.log(this.props.match.params.myid);
this.props.dispatch({
type:"maizuo/hide"
}
)
}
componentWillUnmount(){
this.props.dispatch({
type:"maizuo/show"
})
}
render() {
return (
<div>Detail</div>
)
}
}
export default connect()(Detail)
|