0

I have to pass few values on clicking a button in ReactJS Functional Component, below is the code snippet.

<span className="edit" onClick={ onClickEdit(value.title, value.details)}> 
   <img src={editImg} height="15" width="15" />
</span>

This is showing an error. As I know we can't use 'this' keyword in Functional Component. Please help with this.

1
  • onClick={() => onClickEdit(value.title, value.details)} Commented Nov 28, 2019 at 11:25

3 Answers 3

1

Try this

import React from 'react';
import SearchBar from './SearchBar';

const App = () => {
return (
<div className="ui container" style={{ marginTop: '10px' }}>
    {/* <SearchBar /> */}
    <button onClick={()=>testFunc('gi','jgj')}>click me</button>
</div>

)}

const testFunc = (a, b) => {
console.log(a, b);
}


export default App;
Sign up to request clarification or add additional context in comments.

Comments

0

You can call your function like below snippet

<Button variant="info" onClick={() => editUser(userObject)}>Edit</Button>

Comments

0

The onClick props expects a function. By doing what you are doing, you are not passing a function, but calling a function directly in the prop.

You can pass an anonymous function (a function that is not declared) directly in the prop:

<span className="edit" onClick={() => onClickEdit(value.title, value.details)}> 
   <img src={editImg} height="15" width="15" />
</span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.