How do I reset the Textfield in my <Search/>
Component when I click the Logo in my <Header />
Component?
The <Search/>
Component is already entangled with another Component through Callbacks and props.
I’ve tried custom Events, creating a higher Level State and passing the Event up through a Callback and then down again through a prop. This led to glitches and infinite rerenders or Invalid Hook Calls.
Whats the proper and best way to Pass this "Clear Event" to the <Search/>
Component without running into those issues?
2
Answers
I’d use useContext for handling the state of a small app.
Here’s a working example
The Context API is how you get shared variables / functions between components.
You can put the value of the search bar into a context to allow other components to modify it.
Stackblitz: https://stackblitz.com/edit/stackblitz-starters-jxsehh?file=src%2Findex.tsx
Snippet: