これは些細なことだと確信していますが、ユーザーがボタンをクリックしたときにボタンの値にアクセスする方法を理解できないようです。ページがロードされると、ボタンのリストが一意の値で正しくレンダリングされます。ただし、ボタンの1つをクリックすると関数が起動しますが、値は未定義です。私がここで間違っていることを誰かに見せてもらえますか?ボタンコンポーネントの値を取得できませんonClick
パス:TestPage.jsx
import MyList from '../../components/MyList';
export default class TestPage extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.handleButtonClick = this.handleButtonClick.bind(this);
}
handleButtonClick(event) {
event.preventDefault();
console.log("button click", event.target.value);
}
render() {
return (
<div>
{this.props.lists.map((list) => (
<div key={list._id}>
<MyList
listCollection={list}
handleButtonClick={this.handleButtonClick}
/>
</div>
))}
</div>
);
}
}
パス:MyListComponent
const MyList = (props) => (
<div>
<Button onClick={props.handleButtonClick} value={props.listCollection._id}>{props.listCollection.title}</Button>
</div>
);