Как передать функцию от родителя к ребенку, если у вас уже есть проптипы в ReactJS - PullRequest
0 голосов
/ 29 мая 2020

Я пытаюсь передать метод в качестве свойств дочернему классу. Ошибка: this.props.funcName не является функцией.

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,
};

Дочерний

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));

Когда я пытаюсь вызвать функцию в моем дочернем классе как «this.props.funcName ()», она показывает, что this.props.funcName не функция. Я также использую prop-types . Это влияет на код? Когда я написал программу hello world и реализовал это наследование родитель-потомок, она работала хорошо.

Я совершенно ничего не понимаю.

this.props в видеокомпоненте

this.porps in Video Component

1 Ответ

2 голосов
/ 29 мая 2020

Вы передали его как props funcName. Так что вы можете называть это this.props.funcName()

...