score:81
Accepted answer
The ES6 way:
Using arrow functions =>
const items = this.props.items.map((item) => (
<ul key={item.id}>
<li>
<button onClick={() => this.displayAlert(item.email)}>
{item.lastName + ', ' + item.firstName}
</button>
</li>
</ul>
));
onClick
the anonymous function is called and executes this.displayAlert(item.email)
The ES5 way:
You could do this using .bind
and passing the parameter in there.
You should also pass this
or use bind to keep the proper context on your .map
function:
var items = this.props.items.map(function(item) {
return (
<ul key={item.id}>
<li>
<button onClick={this.displayAlert.bind(this, item.email)}>
{item.lastName + ', ' + item.firstName}
</button>
</li>
</ul>
);
}, this);
Shown in the example here: React - Communicate Between Components
score:4
Using arrow function and babel plugin "transform-class-properties"
class People extends React.Component {
render() {
return (
<ul>
{ this.props.items.map( (item) => (
<li key={item.id}>
<button onClick={this.displayAlert(item)}>
{item.lastName + ', ' + item.firstName}
</button>
</li>
))}
</ul>
)
}
displayAlert = (item) => (event) => {
// you can access the item object and the event object
alert('Hi');
}
}
Source: stackoverflow.com
Related Query
- How to pass arguments to functions in React js?
- How to add arguments to event handler in ES6 React when functions are bound in constructor
- How to pass arguments to callback in React + Typescript without new function creation?
- How to pass arguments to function in functional component in React
- How to pass two functions to onClick event in react
- How to avoid using inline functions in React (ES6) for functions that take arguments
- How to pass handlSubmit and handleChange functions from Parent to Child in React
- How to pass parameters in react functions with Typescript
- How to pass cli arguments to react craco?
- How to pass a function with arguments as prop in React without creating an arrow function
- How can I pass arguments in action different 3 level react components without redux and context API?
- React - How to properly pass and invoke functions that accepts parameters to a component
- Warning: Functions are not valid as a React child. or how to pass the id into element in Route tag
- How can I pass arguments to a bound function to a React child?
- React Best Practices: If we can't use arrow functions in JSX onChange( ), how can we pass in custom arguments?
- How to pass down functions as a prop with the new React Hooks
- How to pass functions as props to React Custom Modal?
- React and TypeScript: How to pass Arguments to Event handler of type: MouseEventHandler
- How to pass props from async functions triggered by an OnClick event to a component React js
- React How to pass arguments to function
- how to pass functions through React components
- How to pass multiple arguments through React Context (from Consumer to Provider)?
- How to pass json data from one component to another using functions in react
- How can I pass in the arguments to a function in react from within the html?
- How to pass in a react component into another react component to transclude the first component's content?
- How to create helper file full of functions in react native?
- React - How to pass HTML tags in props?
- React - how to pass state to another component
- How to pass all other props to react class?
- React - How to pass props to a component passed as prop
More Query from same tag
- Button changing color after closing modal
- I want to know how can I access images from next frontend public folder
- Routes not working in react router v4
- Is it possible to dismiss all toasts without animation?
- Resize the windows and get a stylish scroll bar for the menu
- How to create a function that doesn't do anything in React
- How to reference other inputs in the same "row"?
- g6.js ant : Can not switch back and forth between two layouts
- Correct way to require material-ui by webpack
- How can I detect whether a mouse click is long or short?
- (React and Django) I am unable to delete an user from my userlist. (HTTP Status Code 404)
- how to add Inverted triangle shape scatter in rechart
- Why my browser shows blank page when I run express server (React.js)?
- Add sub-routine for Office.js Javascript Word add-in using asynchronous execution context
- react.js useState is not defined
- React Select not showing errors when validating with Formik and Yup
- Why is .card-header not rendering properly in my React Bootstrap setup?
- How can I display elements into each row?
- Using Styled-Map with PolishedJS
- reactjs render array of objects into a list gives an error
- Making Axios post Req from Gatsby JS
- React button hover css pseudoclass not working
- How to change image source using state variable with React.js
- How to handle click on details (and navigate back on list) with infinite list item?
- How to use nested routes to add content to a page without removing the content of the previous route with react-router-v4?
- Made custom checkbox in React
- Defining the data in an array
- Change value of css in components
- why I can't get my onChange/onBlur handler work on custom component (reactjs)
- How to access a public method from a wrapped Component using TypeScript?