javascript - 如何绑定(bind)按钮REACT的值?

标签 javascript reactjs

我不明白为什么我的值是最后一个值data[i].Country什么时候不应该这样。

for (var i = data.length - 1; i >= 0; i--) {
        var test = data[i].Country
        // All good \/
        console.log(test);

        const option = (
                                                           // here is the bug \/                                               
            <button className="btn" value={i} onClick={() => this.dataHandle(test)} >{test}</button>
        );
}

{test}之间>{test}</button>很好。第一个console.log(test)很好。 仅在() => this.dataHandle(test)中不起作用。

dataHandle (country) {
    console.log(country);
}

知道为什么会有这样的行为吗?

我从互联网上尝试了很多方法,但没有一个主题有效:/

编辑:抱歉,我粘贴代码时犯了一个错误

最佳答案

我猜您想将当前值 = {i} 作为数据句柄中的 a 传递。

for (var i = data.length - 1; i >= 0; i--) {
        var test = data[i].Country
        // All good \/
        console.log(test);

        const option = (
                                                           // Maybe this \/                                               
            <button className="btn" value={i} onClick={(e) => this.dataHandle(e.currentTarget, test)} >{test}</button>
        );
}

dataHandle (country) {
    country = country.textContent
}

关于javascript - 如何绑定(bind)按钮REACT的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61427896/

相关文章:

unit-testing - React.js 和 Jasmine Spies

javascript - Transform 属性在 Firefox 中不起作用,但在 Chrome 和 Safari 中起作用。该怎么办?

javascript - 从模块函数内访问调用者脚本的 __filename

javascript - 如何使用 vanilla js 将包含样式标签的字符串按原样附加到文档头?

javascript - 如何在变量 {content} 中的 200 个单词后添加 Admob

javascript - 如何检查有多少 React child 拥有某个 Prop ?

javascript - 如何在 React with Typescript 中使用 Material UI 自定义变体

javascript - 正则表达式允许空格

javascript - 在长页面中间暂时锁定页面滚动

javascript - 如何使用jquery隐藏文本框的值