gpt4 book ai didi

reactjs - 将多个参数传递给 React 中的事件处理程序

转载 作者:行者123 更新时间:2023-12-03 13:33:04 24 4
gpt4 key购买 nike

所以我试图让 React 使用 ES6 语法。在 ES5 中,我使用了 setInitialState,但没有使用函数绑定(bind)语法的构造函数。我有一个任意的价格列表,我希望当输入元素更改时状态也会更改。但正确的价格必须改变。

我什至不太确定这是正确的方法。有人可以告诉我最新的方法吗?

这是我的代码:

import React, {Component} from 'react'
import 'bootstrap/dist/css/bootstrap.css';

export default class PriceTable extends Component {
constructor(props, context) {
super(props, context);

this.state = {
pid: this.props.pid || "12345",
name: this.props.name || "name",
prices: this.props.prices || [
{count: 1, desc: 'one', price: 8.25},
{count: 6, desc: 'six', price: 7.60},
{count: 12, desc: 'twelve', price: 6.953}
]
};

this.setPid = this.setPid.bind(this);
this.setName = this.setName.bind(this);
this.setCount = this.setCount.bind(this, i);
this.setDesc = this.setDesc.bind(this, i);
this.setPrice = this.setPrice.bind(this, i);
this.logDebug = this.logDebug.bind(this);
}

setPid(e) {
this.setState({pid: e.target.value})
}

setName(e) {
this.setState({name: e.target.value})
}

setCount(i, e) {
var newPrices = this.state.prices
newPrices[i].count = e.target.value
this.setState({prices: newPrices})
}

setDesc(i, e) {
var newPrices = this.state.prices
newPrices[i].sec = e.target.value
this.setState({prices: newPrices})
}

setPrice(i, e) {
var newPrices = this.state.prices
newPrices[i].price = e.target.value
this.setState({prices: newPrices})
}

_renderPriceRow(price, i) {
return (
<tr key={i}>
<td >
<input type="text" className="form-control" defaultValue={price.count} onChange={this.setCount(this, i).bind(this, i)}/>
</td>
<td >
<input type="text" className="form-control" defaultValue={price.desc} onChange={this.setDesc(this, i).bind(this, i)}/>
</td>
<td >
<input type="text" className="form-control" defaultValue={price.price} onChange={this.setPrice(this, i).bind(this, i)}/>
</td>
</tr>
);
}

render() {
return (
<div className="row">
...
</div>
);
}
}

这就是错误......

PriceTable.jsx:21 Uncaught ReferenceError: i is not defined
at new PriceTable (PriceTable.jsx:21)

最佳答案

您错误地绑定(bind)了函数

在你的构造函数中,你不需要指定参数,你只需要像 this.setDesc = this.setDesc.bind(this); 那样绑定(bind)它。

此外,在 onChange 中,当您想要将参数传递给函数时,您可以使用绑定(bind)指定它,而不是作为参数传递并再次绑定(bind)它们。

onChange={this.setDesc.bind(this, i)}

整个代码看起来像这样

import React, {Component} from 'react'
import 'bootstrap/dist/css/bootstrap.css';

export default class PriceTable extends Component {
constructor(props, context) {
super(props, context);

this.state = {
pid: this.props.pid || "12345",
name: this.props.name || "name",
prices: this.props.prices || [
{count: 1, desc: 'one', price: 8.25},
{count: 6, desc: 'six', price: 7.60},
{count: 12, desc: 'twelve', price: 6.953}
]
};

this.setPid = this.setPid.bind(this);
this.setName = this.setName.bind(this);
this.setCount = this.setCount.bind(this);
this.setDesc = this.setDesc.bind(this);
this.setPrice = this.setPrice.bind(this);
this.logDebug = this.logDebug.bind(this);
this._renderPriceRow = this._renderPriceRow.bind(this);
}

setPid(e) {
this.setState({pid: e.target.value})
}

setName(e) {
this.setState({name: e.target.value})
}

setCount(i, e) {
var newPrices = this.state.prices
newPrices[i].count = e.target.value
this.setState({prices: newPrices})
}

setDesc(i, e) {
var newPrices = this.state.prices
newPrices[i].sec = e.target.value
this.setState({prices: newPrices})
}

setPrice(i, e) {
var newPrices = this.state.prices
newPrices[i].price = e.target.value
this.setState({prices: newPrices})
}

_renderPriceRow(price, i) {
return (
<tr key={i}>
<td >
<input type="text" className="form-control" defaultValue={price.count} onChange={this.setCount.bind(this, i)}/>
</td>
<td >
<input type="text" className="form-control" defaultValue={price.desc} onChange={this.setDesc.bind(this, i)}/>
</td>
<td >
<input type="text" className="form-control" defaultValue={price.price} onChange={this.setPrice.bind(this, i)}/>
</td>
</tr>
);
}

render() {
return (
<div className="row">
...
</div>
);
}
}

您还可以使用 Arrow functions 传递参数,例如

onChange={(e) => this.setDesc(e,  i)}

关于reactjs - 将多个参数传递给 React 中的事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43155329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com