0

Developed with react and typescript.
Now the card is shown or hidden when you click on the div tag.
I want to hide the Card when it is displayed, even if another place other than the div tag is pressed.

import React, { FunctionComponent, useState } from 'react';
import { Card } from 'components/atoms/Card';
import { Display } from 'components/atoms/Display';
const Test: FunctionComponent = () => {
  const [isDisplay, setIsDisplay] = useState(false);

  const onClick = () => {
    setIsDisplay(!isDisplay);
  };
  return (
    <>
      <div onClick={onClick} style={{ width: '100px', height: '100px' }}>
        display Card
      </div>
      <Display enabled={isDisplay}>
        <Card width={100} height={100}></Card>
      </Display>
    </>
  );
};

export default Test;
iiiuuuu
  • 11
  • 2

1 Answers1

0

Try this in your onClick method. It looks like you need to access the current state's value and update it.

setIsDisplay(state => !state);

It's explained here in the React docs. https://reactjs.org/docs/hooks-reference.html#functional-updates

Joseph Gordy
  • 106
  • 1
  • 7