Loading...
「ツール」は右上に移動しました。
利用したサーバー: wtserver1
0いいね 6 views回再生

How to Pass Values to React onClick Method: Best Solution

React js onClick can't pass value to method

1. Using Arrow Function:

button onClick={() = myMethod('value')} Click Me /button

In this example, myMethod will receive 'value' as its argument when the button is clicked.

2. Binding in the Constructor (Class Component Only):

class MyComponent extends React.Component {
handleClick(value) {
// Access the value here
console.log(value);
}

render() {
return (
button onClick={() = this.handleClick('value')} Click Me /button
);
}
}

This method ensures that the correct context (this) is maintained and the value is passed to the method.

コメント