ReferenceError : Dispatch is not defined
ReferenceError : Dispatch is not defined
我有一个连接器,它有 mapDispatchToProps 和 mapStateToProps 函数,我需要从我的主要组件调度一个动作。
我在尝试发送时收到一条错误消息,提示未定义发送 fetchPlaces(this.props.user.id)
this.props.user.id
的值为 1。
我需要获取用户 ID 并将其传递给 fetchPlaces,该实习生会为我获取用户的位置。我不知道该怎么做。
连接器
const mapStateToProps = function (store) {
return {
elements: store.elements.elements,
places: store.places.places,
geocode : store.geocode.geocode,
user : store.user.user
};
};
const mapDispatchToProps = function (dispatch) {
return {
userAction : dispatch(fetchUser()),
elementsAction : dispatch(fetchCategories()),
placesAction: (id) => { dispatch(fetchPlaces(id)) }
}
}
class BasicinfoConnector extends React.Component{
render() {
console.log(this.props.user);
if(typeof this.props.user.id != "undefined"){
return (
<Basicinfo elements={this.props.elements} places={this.props.places} geocode={this.props.geocode} user={this.props.user}/>
);
}
else{
return null;
}
}
}
export default Redux.connect(mapStateToProps, mapDispatchToProps)(BasicinfoConnector);
组件:
componentWillMount() {
console.log(this.props);
console.log(this.props.user.id);
dispatch(fetchPlaces(this.props.user.id))
}
placesAction: (id) => { dispatch(fetchPlaces(id)) }
这样做的语法正确吗?
更新
我更改了 componentWillMount
:
componentWillMount() {
console.log(this.props);
console.log(this.props.user.id);
dispatch(this.props.placesAction(this.props.user.id))
}
和 mapDispatchToProps :
const mapDispatchToProps = function (dispatch) {
return {
userAction: bindActionCreators(fetchUser, dispatch),
elementsAction: bindActionCreators(fetchUser, dispatch),
placesAction: (id) => { dispatch(fetchPlaces(id)) }
}
}
还是一样的错误。
您需要将 属性 传递到下一个级别,或者像这样分享您的所有道具:
<Basicinfo {...this.props} />
或仅您想要的特定
<Basicinfo placesAction={(id) => this.props.placesAction(id)} />
我有一个连接器,它有 mapDispatchToProps 和 mapStateToProps 函数,我需要从我的主要组件调度一个动作。
我在尝试发送时收到一条错误消息,提示未定义发送 fetchPlaces(this.props.user.id)
this.props.user.id
的值为 1。
我需要获取用户 ID 并将其传递给 fetchPlaces,该实习生会为我获取用户的位置。我不知道该怎么做。
连接器
const mapStateToProps = function (store) {
return {
elements: store.elements.elements,
places: store.places.places,
geocode : store.geocode.geocode,
user : store.user.user
};
};
const mapDispatchToProps = function (dispatch) {
return {
userAction : dispatch(fetchUser()),
elementsAction : dispatch(fetchCategories()),
placesAction: (id) => { dispatch(fetchPlaces(id)) }
}
}
class BasicinfoConnector extends React.Component{
render() {
console.log(this.props.user);
if(typeof this.props.user.id != "undefined"){
return (
<Basicinfo elements={this.props.elements} places={this.props.places} geocode={this.props.geocode} user={this.props.user}/>
);
}
else{
return null;
}
}
}
export default Redux.connect(mapStateToProps, mapDispatchToProps)(BasicinfoConnector);
组件:
componentWillMount() {
console.log(this.props);
console.log(this.props.user.id);
dispatch(fetchPlaces(this.props.user.id))
}
placesAction: (id) => { dispatch(fetchPlaces(id)) }
这样做的语法正确吗?
更新
我更改了 componentWillMount
:
componentWillMount() {
console.log(this.props);
console.log(this.props.user.id);
dispatch(this.props.placesAction(this.props.user.id))
}
和 mapDispatchToProps :
const mapDispatchToProps = function (dispatch) {
return {
userAction: bindActionCreators(fetchUser, dispatch),
elementsAction: bindActionCreators(fetchUser, dispatch),
placesAction: (id) => { dispatch(fetchPlaces(id)) }
}
}
还是一样的错误。
您需要将 属性 传递到下一个级别,或者像这样分享您的所有道具:
<Basicinfo {...this.props} />
或仅您想要的特定
<Basicinfo placesAction={(id) => this.props.placesAction(id)} />