1

I have an App.js which calls a function DisplayQuestions in its render method. What is the right place to declare the handleClick() function? I tried to declare it inside of DisplayQuestions but that never seems to get called. I looked at this similar issue but couldnt get the binding working like this question, possibly because the DisplayQuestions component is a function (not part of App.js).

The button displays fine, but clicks are never logged to console. I have tried creating the button using the event property and the onclick property with the same result:

class App extends React.Component {
  render() {
    return (
      <div className="App"></div>
          <DisplayQuestions />
    )
  }

DisplayQuestions:

export function DisplayQuestions() {
    const [notes, setNotes] = useState([{ questions: [], definition: "" }]);
    const [query, setNotesQuery] = useState("Dynamo");
    const [isLoading, setIsLoading] = useState(true);

    // This binding is necessary to make `this` work in the callback
    this.handleClick = this.handleClick.bind(this);
    //handleClick function
    var handleClick = (q) => {
        console.log(`handleClick`, q)
    }

    useEffect(() => {
        const fetchData = async () => {
            setIsLoading(true);
            setNotes(await NotesApiQuery(query))
            setIsLoading(false);
        };
        fetchData();
    }, [query]);

    return (
        <div>
            {isLoading ? (
                <div> Loading ...</div>
            ) :
                <div>
                    <h3>Notes({notes.length}):</h3>
                    {
                        notes.map((n) => (
                            <div>
                                <li>
                                    <span style={{ visibility: 'hidden' }} id={n.questions}>{n.definition}</span>
                                    <button event={handleClick(n.questions)}>Show/Hide ans</button>
                                </li>
                            </div>
                        )
                        )
                    }
                </div>
            }
        </div>
    );
}
3
  • That App code won't compile. JSX doesn't allow multiple top-level elements. Commented Feb 3, 2021 at 15:17
  • "This binding is necessary to make this work in the callback" That's for class components, not functional components using hooks. Commented Feb 3, 2021 at 15:18
  • The first hit when I google: react button click is reactjs.org/docs/handling-events.html and it has a short example above the fold. <button onClick={activateLasers}> Activate Lasers </button> Commented Feb 3, 2021 at 15:43

2 Answers 2

1

You're not hooking up the function correctly, and you have extra code you've probably put there because you saw it in class components. But yours is a functional component, not a class component.

Here's how to hook it up correctly:

<button onClick={() => handleClick(n.questions)}>Show/Hide ans</button>

Note that uses onClick, not event, and wraps the call to handleClick(n.questions) in an arrow function.

Then remove this statement and the comment above it: this.handleClick = this.handleClick.bind(this);

Sign up to request clarification or add additional context in comments.

Comments

0

you can try binding like below

<button onclick={()=>handleClick(n.questions)}>Show/Hide ans</button>

1 Comment

button has no event property. The user would also need to change that to onClick (as in my answer).

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.