2

I'm kind of new to React and I'm having difficulty in invoking components through onClick event of a button.

Given below is the code segment

function Dev({value}){
    return(
        <Card>
            <CardImg top width="100%" src={value.src} alt="Card image cap" />
            <CardBody>
                <CardTitle className='text-card'>{value.title}</CardTitle>
                <CardText className='text-text'>{value.text}</CardText>
                <Button className='text-button' onClick={(value)=>{
                    return(
                        <div>
                            {value.id ? <Login /> : <Sign />}
                        </div>
                    )
                }}>{value.button}<i class={value.icon} aria-hidden="true"></i></Button>
            </CardBody>
        </Card>
);
}

value.id is either true or false, but either way the onClick is not firing.

The components Login and Sign are actually components consisting of a Modal.

Some help is deeply appreciated.

5
  • 3
    Returning JSX from the click function doesn't make any sense. What do you want to happen when the user clicks on the button? Commented Nov 17, 2020 at 13:38
  • To render the Login/Sign component Commented Nov 17, 2020 at 13:41
  • Set a state when the button is clicked, show the components when the state is set. Look up state and conditional rendering in the docs. Commented Nov 17, 2020 at 13:43
  • So updating the state is the only way to render the components? Commented Nov 17, 2020 at 13:45
  • 2
    State is the best way to do it, yes. Returning from onClick doesn't work, if it did, where would it be rendered? Would it replace the button? The Component? Get added to the existing JSX somewhere? Basically, there is no logical way for returning JSX from the onClick to work (thankfully it doesn't). So setting a flag in state and conditionally rendering is the way to go. Commented Nov 17, 2020 at 14:19

1 Answer 1

2

To solve your problem you should add state for you component and keep something isButtonClicked in it:

function Dev({ value }) {
  const [isButtonClicked, setIsButtonClicked] = useState(false);

  const handleClick = () => setIsButtonClicked(true);

  return (
    <Card>
      <CardImg top width="100%" src={value.src} alt="Card image cap" />
      <CardBody>
        <CardTitle className="text-card">{value.title}</CardTitle>
        <CardText className="text-text">{value.text}</CardText>
        <Button className="text-button" onClick={handleClick}>
          {value.button}
          <i class={value.icon} aria-hidden="true"></i>
        </Button>
        {isButtonClicked && (value.id ? <Login /> : <Sign />)}
      </CardBody>
    </Card>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

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.