react-modal-hook icon indicating copy to clipboard operation
react-modal-hook copied to clipboard

React Hook "useState" cannot be called inside a callback.

Open singleseeker opened this issue 4 years ago • 1 comments

const [showModal] = useModal(() => {
    const [count, setCount] = useState(0);

    return (
      <ReactModal isOpen>
        <span>The count is {count}</span>
        <button onClick={() => setCount(count + 1)}>Increment</button>
      </ReactModal>
    );
  });

  return <button onClick={showModal}>Show modal</button>;

when I am trying to use demo code, got an error.

React Hook "useState" cannot be called inside a callback.

singleseeker avatar Mar 20 '21 00:03 singleseeker

I did the workaround by define the whole component outside, then use like useModal(() => <MyComponent count={count} />, [count])

sangdth avatar Apr 28 '21 14:04 sangdth