亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何解決錯誤“this.props.myFunction”不是react中的函數?

如何解決錯誤“this.props.myFunction”不是react中的函數?

智慧大石 2021-09-30 14:01:00
我有一個包含兩個組件的 React 應用程序。我的子組件有一個包含一些記錄的表。每條記錄都有一個按鈕,我在 onclick 中調用一個帶有參數 (id) 的函數 (editUser)。在該函數中,我通過 props 將該 id 發送到父組件。該函數已在構造函數中綁定。但是當按鈕點擊時,控制臺說this.props.userId 不是一個函數子組件:class UsersTable extends React.Component {    constructor(props) {        super(props);        this.state = {        };        this.editUser = this.editUser.bind(this);    }    editUser(id) {       this.props.userId(id);    }    render() {        return (        <TableBody>            {data                .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)                .map(n => {                    return (                        <TableRow key={n.id}>                            <TableCell className={classes.fixedTd}>                                <BorderColorIcon onClick={() => this.editUser(n.userId)} className="action margin-r" />                            </TableCell>                        </TableRow>                    );                })}        </TableBody>        );    }}export UsersTable;父組件:class CompanyUserMgt extends Component {    constructor(props) {        super(props);        this.state = {        }    }    editUser = (id) => {        console.log("user", id)    }    render() {        return (            <div className="">        <UsersTable            userId={this.editUser}                    key={this.state.tableKey}                />            </div>        )    }}export default CompanyUserMgt;我該如何解決這個問題?
查看完整描述

1 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

我認為您可以簡化事情,您可以在您的子組件上嘗試以下代碼嗎?


class UsersTable extends React.Component {

    constructor(props) {

        super(props);

        this.state = {

        };


    }


    render() {

        return (

        <TableBody>

            {data

                .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)

                .map(n => {

                    return (

                        <TableRow key={n.id}>

                            <TableCell className={classes.fixedTd}>

                                <BorderColorIcon onClick={() => this.props.userId(n.userId)} className="action margin-r" />

                            </TableCell>

                        </TableRow>

                    );

                })}

        </TableBody>

        );

    }

}


export UsersTable;


查看完整回答
反對 回復 2021-09-30
  • 1 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號