当ReactJS中已经有proptypes时,如何将函数从父级传递给子级
我试图将一个方法作为道具传递给子类。 错误:this.props.funcName不是函数当ReactJS中已经有proptypes时,如何将函数从父级传递给子级,reactjs,jsx,react-props,Reactjs,Jsx,React Props,我试图将一个方法作为道具传递给子类。 错误:this.props.funcName不是函数 class Poll extends Component { constructor(props) { super(props); this.state = { customPollReq: false, isPolling: false, customPollValues: [], }; this.inputEditor = []
class Poll extends Component {
constructor(props) {
super(props);
this.state = {
customPollReq: false,
isPolling: false,
customPollValues: [],
};
this.inputEditor = [];
this.renderCustomMView = this.renderCustomMView.bind(this);
}
.
.
.
renderCustomMView() {
const { intl, startCustomPoll } = this.props;
const isDisabled = _.compact(this.inputEditor).length < 1;
if (this.inputEditor.length > 0) {
Session.set('pollInitiated', true);
this.setState({ isPolling: true }, () =>
startCustomPoll('custom', _.compact(this.inputEditor)));
}
render(){
return(
<div>
<Video funcName={this.renderCustomMView} />
</div>
const poll = Poll;
const withInjectIntl = injectIntl(Poll);
export { poll as Poll };
export default withModalMounter(withInjectIntl);
Poll.propTypes = {
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
amIPresenter: PropTypes.bool.isRequired,
pollTypes: PropTypes.instanceOf(Array).isRequired,
startPoll: PropTypes.func.isRequired,
startCustomPoll: PropTypes.func.isRequired,
stopPoll: PropTypes.func.isRequired,
};
当我尝试以“this.props.funcName()”的形式调用子类中的函数时,它显示this.props.funcName不是函数。我还使用了道具类型。这会影响代码吗?当我编写hello world程序并实现这个子-父继承时,它工作得很好
我完全不懂
这是视频组件中的道具
您将其作为funcName道具传递。所以你可以称它为this.props.funcName()你也可以发布视频组件吗?我发现了一个错误。您传递了它
this.rendercutomview
,而不是this.rendercutomview
。这是打字错误的问题,不是吗?我的错。在复制代码时,我错过了“M”。是this.renderConstomView。请在console.Done中发布您所记录的视频组件的this.props
。请检查一下。
class VideoPlayer extends Component {
constructor(props) {
super(props);
.
.
.
const { isPresenter } = props;
this.newFuncCall = this.newFuncCall.bind(this);
.
.
.
newFuncCall(){
console.log("12345");
this.props.funcName();
}
.
.
const video = VideoPlayer;
export {video as Video};
export default injectIntl(injectWbResizeEvent(VideoPlayer));