Javascript React演示文稿组件未从智能组件接收MapDispatchToProps
这真是奇怪和尴尬&我似乎不明白这里出了什么问题 以下是“智能”组件:Javascript React演示文稿组件未从智能组件接收MapDispatchToProps,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,这真是奇怪和尴尬&我似乎不明白这里出了什么问题 以下是“智能”组件: import { connect } from "react-redux"; import TemplateParent from "../component/svg/TemplateParent"; import { setInitialViewBox } from "../modcon/Actions"; const mapStateToProps = ({ svgTemplateState }) => {
import { connect } from "react-redux";
import TemplateParent from "../component/svg/TemplateParent";
import { setInitialViewBox } from "../modcon/Actions";
const mapStateToProps = ({ svgTemplateState }) => {
return({
renderType : "Template Selection",
svgTemplateState : svgTemplateState
});
};
const mapDispatchToProps = ( dispatch ) => {
return({
dispatchSetInitialViewBox : ( viewBoxCoOrdinates ) => {
dispatch( setInitialViewBox( viewBoxCoOrdinates ));
}
});
};
const C_TemSelectionSVGTemplate = connect( mapStateToProps, mapDispatchToProps )( TemplateParent );
export default C_TemSelectionSVGTemplate;
import React from "react";
import { calcViewBoxCentre } from "../../modcon/GlobalAPI";
const TemplateParent = React.createClass({
componentDidUpdate : function(){
let getEleDimension = this.gEle.getBBox(),
viewboxValue = calcViewBoxCentre( getEleDimension, 1.75 );
/*** ERROR IS HERE: this.props.dispatchSetInitialViewBox is not a function ***/
this.props.dispatchSetInitialViewBox( viewboxValue );
},
render : function(){
/*** the below console logs only shows two props: `renderType` & `svgTemplateState` ***/
console.log( this.props );
return(
<g className = "templateParent"
ref = {( g ) => this.gEle = g }>
</g>
);
}
});
以下是“演示”部分:
import { connect } from "react-redux";
import TemplateParent from "../component/svg/TemplateParent";
import { setInitialViewBox } from "../modcon/Actions";
const mapStateToProps = ({ svgTemplateState }) => {
return({
renderType : "Template Selection",
svgTemplateState : svgTemplateState
});
};
const mapDispatchToProps = ( dispatch ) => {
return({
dispatchSetInitialViewBox : ( viewBoxCoOrdinates ) => {
dispatch( setInitialViewBox( viewBoxCoOrdinates ));
}
});
};
const C_TemSelectionSVGTemplate = connect( mapStateToProps, mapDispatchToProps )( TemplateParent );
export default C_TemSelectionSVGTemplate;
import React from "react";
import { calcViewBoxCentre } from "../../modcon/GlobalAPI";
const TemplateParent = React.createClass({
componentDidUpdate : function(){
let getEleDimension = this.gEle.getBBox(),
viewboxValue = calcViewBoxCentre( getEleDimension, 1.75 );
/*** ERROR IS HERE: this.props.dispatchSetInitialViewBox is not a function ***/
this.props.dispatchSetInitialViewBox( viewboxValue );
},
render : function(){
/*** the below console logs only shows two props: `renderType` & `svgTemplateState` ***/
console.log( this.props );
return(
<g className = "templateParent"
ref = {( g ) => this.gEle = g }>
</g>
);
}
});
我读了你的代码,但没发现你的问题, 但我有一些注意: 不应在mapDispatchToProps中传递参数
viewBoxCoOrdinates
。
您可以检查bindActionCreators可能是解决方案
例如:
import {bindActionCreators} from 'react-redux';
const mapDispatchToProps = ( dispatch ) => {
return({
dispatchSetInitialViewBox : bindActionCreators(viewBoxCoOrdinates, dispatch)
}
});
};
您可以通过props访问函数
更多信息:您可以使用速记符号将分派映射到以下道具:
export default connect( mapStateToProps, { setInitialViewBox })( TemplateParent );
在您的组件中,您可以执行
这个.props.setInitialViewBox
不一定相关,但我认为您正在导出TemplateParent组件?是的。我必须简化演示组件TemplateParent
,因为其中有一些计算。但是,是的,它是导出默认模板父级
向我们展示设置初始视图框
操作我诚挚的道歉,事实证明该组件在两个不同的地方使用。一旦它连接到智能组件
(如上所述)&第二次,作为普通的子表示组件。它们实际上是同时调用的,用于更新不同的部分,因此会造成混乱。非常感谢大家对代码的审阅。谢谢你的回答,但是,我不确定这会给代码带来什么好处。在我的代码中,TemplateParent
是redux感知的,因此我认为传递viewBoxCoOrdinates
不会有任何问题。如果我遗漏了什么,请纠正我。我已经阅读了丹的页面链接上面,但是,没有看到的好处。