我有一个组件,在我的组件中我有一些子组件。
在父组件中,我有一些函数,我想从子组件触发它。所以我和雷德克斯一起去。
它是我的父组件:
import React, { Component } from "react";
import { withRouter } from "react-router-dom";
import { bindActionCreators } from "redux";
import { splashStop } from "store/actions/Home/splashStop";
import { connect } from "react-redux";
class Home extends Component {
constructor(props) {
super(props);
this.state = {
};
this.goPage = this.goPage.bind(this);
}
componentDidMount() {
}
goPage = () => {
this.props.history.push("/agencies");
};
render() {
if (this.props.homeSplash.splashStart == true) {
myTime.play();
}
return (
<div>
<ChildComponent />
</div>
);
}
}
const mapStateToProps = state => ({
homeSplash: state.homeSplash
});
function mapDispatchToProps(dispatch) {
return {
splashStop: bindActionCreators(splashStop, dispatch)
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(withRouter(Home));
这是我的孩子部分:
下面是我在onClick函数的子组件中分派redux操作:
triggerSplash = () => {
this.props.splashStart();
};
我的行动:
导出const START_SPLASH=
“开始飞溅”;
export const splashStart = () => {
return dispatch => {
dispatch({
type: START_SPLASH,
payload: true
});
};
};
还有我的减速机:
import { START_SPLASH } from "store/actions/Home/splashStart";
let initialState = {
splashStart: false
};
export default (state = initialState, action) => {
switch (action.type) {
case START_SPLASH:
return { ...state, splashStart: action.payload };
default:
return state;
}
};
我的减速器,动作正常。
我在想为什么
myTime.play();
总是在组件安装时工作,只是不关心此控件:
if (this.props.homeSplash.splashStart == true) {
myTime.play();
}
我放错地方了还是怎么了?