【发布时间】:2020-05-02 20:17:25
【问题描述】:
我正在尝试将调度映射到道具,但无论我尝试什么,它都不起作用。
道具不包含我试图传递的功能。
这是我的代码:
(/src/components/containers/CommentForm.js)
import CommentForm from '../ui/CommentForm';
import { withRouter } from 'react-router';
import { connect } from 'react-redux';
import { addComment } from '../../actions';
const mapStateToProps = (state) => ({
showModal: state.showModal,
video: state.video
});
const mapDispatchToProps = dispatch => ({
onNewComment({ username, text }) {
dispatch(addComment(username, text));
}
});
const Container = connect(mapStateToProps, mapDispatchToProps)(CommentForm);
export default withRouter(Container);
我在另一个工作正常的容器中有大约相同的代码。当我在导入的“../ui/CommentForm”文件的构造函数中使用 console.log(props) 时,我只得到 CommentForm 标签中设置的道具:
<CommentForm video={video} showModal={this.state.commentsAllowed && this.state.showCommentForm} toggleCommentForm={this.toggleCommentForm} />
函数 onNewComment 不存在:
{video: {…}, showModal: false, toggleCommentForm: ƒ}
我已经研究了几个小时,但未能找到解决方案。任何帮助深表感谢。谢谢。
更新:
(/src/components/ui/CommentForm.js)
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Modal from "react-bootstrap/Modal";
export default class CommentForm extends Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this._username = React.createRef();
this._text = React.createRef();
this.state = props.video;
console.log(props);
}
handleSubmit = (event) => {
event.preventDefault();
/**
* New comment
*/
this.props.video.comments.push({'username': event.target.username.value, 'text': event.target.text.value});
this.setState({video: this.props.video});
this.props.onNewComment({
username: event.target.username.value,
text: event.target.text.value
});
this.props.toggleCommentForm();
}
render() {
return (
<Modal show={this.props.showModal} centered>
<form className="comment-form" onSubmit={this.handleSubmit}>
<Modal.Header>
<h5 className="modal-title">Comment Form</h5>
<button type="button" className="close" onClick={this.props.toggleCommentForm} data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</Modal.Header>
<Modal.Body>
<label className="row form-group">
<div className="col-12 col-sm-4">Username:</div>
<div className="col-12 col-sm-8"><input className="form-control" type="text" id="username" placeholder="Your Username" ref={input => (this._username = input)} required /></div>
</label>
<label className="row form-group">
<div className="col-12 col-sm-4">Comments:</div>
<div className="col-12 col-sm-8"><textarea className="form-control" id="text" placeholder="Your comments" ref={input => (this._text = input)} required /></div>
</label>
</Modal.Body>
<Modal.Footer>
<button className="btn btn-primary">Submit</button>
</Modal.Footer>
</form>
</Modal>
);
}
}
CommentForm.propTypes = {
onNewComment: PropTypes.func
}
因此,this.props.onNewComment 失败并出现以下错误:
Uncaught TypeError: _this.props.onNewComment is not a function
【问题讨论】:
-
你想在哪里使用它是你导入装饰组件,还是原来的
CommentForm组件?您可以使用您尝试使用表单并记录其道具的组件的代码来更新您的问题吗? -
我刚刚更新了我的问题。
-
我认为您在某处导入未修饰的组件,即 "/src/components/ui/CommentForm.js" 而不是 "/src/components/containers/CommentForm.js" 。如果您在“/src/components/ui/CommentForm.js”中将
onNewComponent设为必需的道具,它可能有助于阐明未通过的地方:onNewComment: PropTypes.func.isReuired。它会在控制台中生成一个反应警告。 -
我发现了错误,我确实是这样导入的: import CommentForm from './ui/CommentForm';而不是从 '../containers/CommentForm' 导入 CommentForm;这只是一个被忽视的严重错误,耗时数小时。
-
如果它有价值,它可能有助于不默认导出未修饰的组件并将 HOC 移动到正在修饰的组件文件中(默认导出和/或不为装饰和未装饰的组件赋予相同的名称。
标签: reactjs react-redux