react给Input绑定onChange事件

最近学习react,绑定事件时遇到了一点小问题,直接上代码:

class PageList extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            visible: false
        }
    }

    changeEvent(event) {
        const name = event.target.name;
        const value = event.target.value;

        console.log("name = " + name + ", value =" + value)

        this.setState({
            [name]: value
        })
    }

    render() {
        const { data } = this.props;
        const columns = [
            {
                title: "校区代码",
                dataIndex: "xqdm",
                key: "xqdm"
            },
            {
                title: "校区名称",
                dataIndex: "xqmc",
                key: "xqmc"
            },
            {
                title: "操作",
                key: "action",
                render: (text, item) => {
                    return (
                        <span>
                            <Popconfirm
                                title="确定要删除吗?"
                                onConfirm={() => this.doDelete(item.xqdm)}
                            >
                                <a>删除</a>
                            </Popconfirm>
                            <Divider type="vertical" />
                            <a onClick={() => this.edit(item)}>编辑</a>
                        </span>
                    );
                }
            }
        ];

        return (
            <div>
                <div className="operation-btns">
                    <Button type="primary" onClick={() => this.handleAdd()}>
                        新增
                    </Button>
                </div>

                <DragModal
                    visible = {this.state.visible}
                    title = "校区信息"
                    onOk = {() => {
                        message.success("添加成功");

                        this.setState({
                            visible: false
                        });

                        console.log(this.state)
                    }}
                    onCancel = {() => {
                        this.setState({
                            visible: false
                        });
                    }}
                >
                    校区代码:<Input name = "xqdm" onChange={() => this.changeEvent} />
                    校区名称:<Input name = "xqmc" onChange={() => this.changeEvent} />
                </DragModal>

                <Table loading={false} columns={columns} dataSource={data} />
            </div>
        );
    }
}

以上代码发现绑定事件并没有被执行,控制台也不报错,一时间不知道怎么办,直到看到添加按钮的点击事件,函数调用写了(),于是猜测可能与没有写括号有关。

<div className="operation-btns">
    <Button type="primary" onClick={() => this.handleAdd()}>
        新增
    </Button>
</div>

果然,换一种写法,问题解决了,仔细想一下,通过onChange={() => this.changeEvent}这种方式绑定事件,箭头后面的内容是方法体,方法体中方法没有写圆括号,导致没有调用到changeEvent()方法,但是浏览器控制台并没有提示,由于事件方法都有一个event参数,为了方便,方法定义使用箭头函数的方式。

以下代码为正确代码:

class PageList extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            visible: false
        }
    }

    changeEvent = (event) => {
        const name = event.target.name;
        const value = event.target.value;

        console.log("name = " + name + ", value =" + value)

        this.setState({
            [name]: value
        })
    }

    render() {
        const { data } = this.props;
        const columns = [
            {
                title: "校区代码",
                dataIndex: "xqdm",
                key: "xqdm"
            },
            {
                title: "校区名称",
                dataIndex: "xqmc",
                key: "xqmc"
            },
            {
                title: "操作",
                key: "action",
                render: (text, item) => {
                    return (
                        <span>
                            <Popconfirm
                                title="确定要删除吗?"
                                onConfirm={() => this.doDelete(item.xqdm)}
                            >
                                <a>删除</a>
                            </Popconfirm>
                            <Divider type="vertical" />
                            <a onClick={() => this.edit(item)}>编辑</a>
                        </span>
                    );
                }
            }
        ];

        return (
            <div>
                <div className="operation-btns">
                    <Button type="primary" onClick={() => this.handleAdd()}>
                        新增
                    </Button>
                </div>

                <DragModal
                    visible = {this.state.visible}
                    title = "校区信息"
                    onOk = {() => {
                        message.success("添加成功");

                        this.setState({
                            visible: false
                        });

                        console.log(this.state)
                    }}
                    onCancel = {() => {
                        this.setState({
                            visible: false
                        });
                    }}
                >
                    校区代码:<Input name = "xqdm" onChange={this.changeEvent} />
                    校区名称:<Input name = "xqmc" onChange={this.changeEvent} />
                </DragModal>

                <Table loading={false} columns={columns} dataSource={data} />
            </div>
        );
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值