Dwarves
Memo
Type ESC to close search bar

Invoking component functions in React

What happens if you invoked the component function directly in React?: Let’s take a look at this example:

const ExampleComponent = () =>{
    const [input, setInput] = useState('')
    //do something here
}

export const App = () =>{
    const [show, setShow] = useState(false) 
    return (
        <div>
            <button onClick={() => setShow(!show)}/>
            {show && ExampleComponent()}
        </div>
    )
}
//This will trigger error "Render more hooks than during the previous render"
//Solution: use <ExampleComponent /> instead

So why does this happen?

How to avoid it?