第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試將函數(shù)傳遞給子組件時出現(xiàn)“TypeError: this.props.change

嘗試將函數(shù)傳遞給子組件時出現(xiàn)“TypeError: this.props.change

一只萌萌小番薯 2021-10-29 15:06:13
我正在嘗試將一個函數(shù)傳遞給一個子組件,該函數(shù)將觸發(fā)邏輯,在按下按鈕后在我的網(wǎng)頁上顯示一個簡單的文本表單,但我收到錯誤消息:類型錯誤:this.props.changeFormStatus 不是函數(shù)下面是父組件的代碼:import React from 'react';import ReactDom from 'react-dom';import RenderIcon from "./RenderIcon";import RenderForm from "./RenderForm";class RetroColumn extends React.Component {    constructor(props) {        super(props);        this.state = {formStatus:false};        this.changeFormStatus = this.changeFormStatus.bind(this);    }    changeFormStatus() {        this.setState({formStatus:true});    }    render() {        return (            <div className="column">                <div className="ui segment">                    <h1 className="ui header">                        <RenderIcon iconName="minus" iconMeaning="Remove"/>                        {this.props.columnName}                        <RenderIcon iconName="plus" whenUserClicks={this.changeFormStatus} iconMeaning="Add"/>                    </h1>                    <RenderForm revealForm={this.state.formStatus}/>                </div>            </div>        );    }};export default RetroColumn;這是子組件的代碼:import React from 'react';import ReactDom from 'react-dom';class RenderIcon extends React.Component{    constructor(props){        super(props);    }    whenUserClicks() {        console.log(this.props);        this.props.changeFormStatus();    };    render() {        return (            <div className="ui vertical animated button" tabIndex="0" onClick={this.whenUserClicks()}>                <div className="hidden content">{this.props.iconMeaning}</div>                <div className="visible content">                    <i className={`${this.props.iconName} icon`}></i>                </div>            </div>        );    }}export default RenderIcon;
查看完整描述

1 回答

?
繁星淼淼

TA貢獻1775條經(jīng)驗 獲得超11個贊

查看您正在呈現(xiàn)并向其傳遞函數(shù)的組件。你寫了這個:

<RenderIcon iconName="plus" whenUserClicks={this.changeFormStatus} iconMeaning="Add"/>

這意味著在 RenderIcon 組件內(nèi)部,該函數(shù)現(xiàn)在被命名為 this.props.whenUserClicks

在您的外部組件中,該函數(shù)名為 this.changeFormStatus。但是在內(nèi)部組件中,它被命名為 this.props.whenUserClicks。

此外,您不是在調(diào)用 this.props.whenUserClicks。您在子組件中創(chuàng)建了一個名為 this.whenUserClicks 的新函數(shù)

此外,在您的子組件中,您有:

  <div onClick={this.whenUserClicks()}></div>

你想把它改成

 onClick={(e)=>{
      this.whenUserClicks()
 }

或者

 onClick={this.whenUserClicks}

如果您像以前一樣調(diào)用該函數(shù),那么該函數(shù)將在頁面加載時立即運行,而不是等待 div 被點擊。


查看完整回答
反對 回復(fù) 2021-10-29
  • 1 回答
  • 0 關(guān)注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號