1

I'm trying to return a component when I click on a button. (a delete button with a confirmation popup) I've tried some examples I found online but none really worked for me.

Here is my code:

class DeleteTask extends React.Component {
  handleClick() {
        return <TaskDeleteModal />
    }
  render() {
    return (
        <div>
            <button type="button" className={styles.deletetask} onClick={() => this.handleClick()}>Verwijderen</button>;
        </div>
    )
  }
}

Thanks for reading, I hope you can assist me.

2 Answers 2

1

It did not work because the return <TaskDeleteModal /> of handleClick does not add <TaskDeleteModal /> to your render function. You need to add <TaskDeleteModal /> to render function and control it visibility by a state: Try the following code:

class DeleteTask extends React.Component {
  this.state = {
    showModal: false
  }
  handleClick() {
        this.setState({showModal: true})
    }
  render() {
    return (
        <div>
            <button type="button" className={styles.deletetask} onClick={() => this.handleClick()}>Verwijderen</button>;
            {/* In TaskDeleteModal you might have a button, when you click on that button call this.setState({showModal: false}) to hide the modal */}
         {this.state.showModal && <TaskDeleteModal />}
        </div>
    )
  }
}

Sign up to request clarification or add additional context in comments.

Comments

0

class DeleteTask extends React.Component {
  this.state = {
    showModal: false
  }
  handleClick() {
        this.setState({showModal: true})
    }
  render() {
    return (
        <div>
            <button type="button" className={styles.deletetask} onClick={() => this.handleClick()}>Verwijderen</button>;
            {/* In TaskDeleteModal you might have a button, when you click on that button call this.setState({showModal: false}) to hide the modal */}
         {this.state.showModal && <TaskDeleteModal />}
        </div>
    )
  }
}

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.