
React子組件回調(diào)父組件函數(shù)傳遞參數(shù).jpg
這個問題呢,還是先研究下師尊給的秘籍比較好
傳參數(shù)
export class EventApp extends React.Component {
deleteById(id, e) {
console.log(`Delete id: ${id}`);
}
render() {
return (
<div>
// 方式一: 使用 bind
<button onClick={this.deleteById.bind(this, 15)}>傳參方式1</button>
// 方式二: 使用箭頭函數(shù)
<button onClick={e => this.deleteById(15, e)}>傳參方式2</button>
</div>
)
}
}
看使用箭頭函數(shù)的方式2:可以知道,其實上面問題中的其實是將事件對象e去掉的一種簡化寫法,因為我們這里并不需要事件對象e;
而直接使用this.props.deleteItem(index),index就相當于方式2里面的事件對象e,沒辦法通過這樣直接調(diào)用傳遞參數(shù)。
()=>{ this.props.deleteItem(index)} ??
(e)=>{this.props.deleteItem(index,e)}