ボタンの配列を作成しましたが、クリックすると各ボタンを処理できるようになりましたが、どのタグがクリックされたかを知るためにthis.handleClickSelection()を処理する方法はわかりません。ReactJSどのタグがクリックされたかを知る方法は?
class Search extends React.Component
{
constructor()
{
super();
this.state = { entries : [], entries_audio : [], }
}
componentWillMount()
{
$.get('https://something', (data) => {
var entriesArray = [];
var entriesAudioArray = [];
for (var i = 0; i < data.stories.length ; i++) {
entriesArray.push(<button className ="entries" key={i} onClick={()=> this.handleClickSelection() } > {data.stories[i].title }</button>);
entriesAudioArray.push(data.stories[i]);
};
this.setState({ indents : entriesArray, entries_audio : entriesAudioArray });
console.log(this.state.entries_audio);
}, 'json');
}
handleClickSelection()
{
alert("this particular button data");
}
//more code below
それは働いている。あなたはなぜonClick = {this.handleClickSelection.bind(this、i)}を知っていますか?動作しますが、onClick = {()=> this.handleClickSelection.bind(this、i)} doesnt? – user2419831
2番目のフォームでクリックハンドラを呼び出さないため、 'bind'は新しいバインドされた関数を返すだけです。そのように書く場合は、ハンドラを呼び出すだけです: 'onClick = {()=> this.handleClickSelection(i)}' – thedude