私はreactでクラステーブルを実装しようとしています。テーブルには複数の行があり、それぞれにボタンがあります。まず、関数handleEventを渡そうとします。
今、私はいつも次のようになります:TypeError:undefinedのプロパティ '_doDeleteItem'を読み取ることができません
なんらかのバインディングエラーがありますが、わかりません。
乾杯
class Table extends Component {
constructor(props,context) {
super(props,context);
this.state = { k : 3 };
this._handleEvent = this._handleEvent.bind(this);
}
_handleEvent (e) {
console.log('delete')
}
render() {
var rows = [];
var lastCategory = null;
this.props.results.slice(-this.state.k).forEach(function(r) {
rows.push( <Rows result = {r} deleteItem = {this._handleEvent} />);
});
return(
<Panel>
<ListGroup fill>
{rows}
</ListGroup>
</Panel>
);
}
}
export default Table;
class Rows extends Component{
render() {
return(
<ListGroupItem>
<b>Text:</b><Button onClick = {this.props.deleteItem} type="False"><b>True</b></Button><br/>
</ListGroupItem>
);
}
}
this
内のコンポーネントインスタンスではありませんforEach
。矢印関数を使用するか、無名関数をバインドするかthis
、2番目の引数としてforEach
:に渡します。
this.props.results.slice(-this.state.k).forEach(function(r) {
rows.push( <Rows result = {r} deleteItem = {this._handleEvent} />);
}.bind(this));
または
this.props.results.slice(-this.state.k).forEach(function(r) {
rows.push( <Rows result = {r} deleteItem = {this._handleEvent} />);
}, this);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加