How to create a modal in ReactJS codepen
Introduction
Let's take a journey together into the world of coding, specifically into using ReactJS to create a modal. Now, I know that for some of you, words like "ReactJS" and "modal" might sound like a bunch of complicated jargon. But don't worry, I promise to explain everything in simple terms as we go along.
What is ReactJS?
First things first, let's break down what ReactJS is. In the simplest terms, ReactJS is a JavaScript library. Now, just like a library is a collection of books, a JavaScript library is a collection of pre-written JavaScript code that we can use to speed up our coding process.
What is a Modal?
A modal is a dialog box or popup window that is displayed on top of the page content. It usually requires users to interact with it before they can go back to using the site. It's a way to get the user's attention and get them to interact with the site in a specific way.
Setting Up Our Development Environment
Before we dive deep into the code, we need to set up our development environment. This is like setting up our kitchen before we start cooking. We need to have all the tools and ingredients ready.
First, we'll need CodePen. CodePen is an online code editor. It's where we'll write our code. Go to the CodePen website and create a new pen.
Next, we need to add ReactJS to our CodePen. To do this, click on the settings icon in the JavaScript section, search for "React" in the JavaScript Preprocessor dropdown, and select it. Do the same for "React-DOM".
Creating a Modal in ReactJS
Now that our kitchen...I mean, our development environment, is set up, let's start cooking...I mean, coding!
Step 1: Creating the Basic Structure
Let's start by creating the basic structure of our modal. The code below will create a simple button that, when clicked, will open our modal.
class App extends React.Component {
render() {
return (
<div>
<button>Open Modal</button>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
In the code above, we have a class called App that extends React.Component. This is the main component of our application. Inside the render method, we have a button with the text "Open Modal".
Step 2: Adding State to Our Component
In React, state is used to store component data that can change over time. In our case, we need to know whether the modal is open or not. So, let's add a state to our App component.
class App extends React.Component {
constructor() {
super();
this.state = {
showModal: false
};
}
render() {
return (
<div>
<button>Open Modal</button>
</div>
);
}
}
In the code above, we added a constructor method to our App component, and inside it, we set the initial state of showModal to false.
Step 3: Creating the Modal Component
Next, we need to create the modal component. This will be a separate component that we'll call from our App component.
class Modal extends React.Component {
render() {
return (
<div>
<h1>This is a modal!</h1>
</div>
);
}
}
In the code above, we have a new component called Modal. Inside the render method, we have a h1 tag with the text "This is a modal!".
Step 4: Displaying the Modal
Now, we need to display the modal when the button is clicked. To do this, we'll add an onClick event to the button, and when the button is clicked, we'll set the state of showModal to true. Then, in the render method, we'll check the state of showModal and display the modal if it's true.
class App extends React.Component {
constructor() {
super();
this.state = {
showModal: false
};
}
openModal = () => {
this.setState({ showModal: true });
}
render() {
return (
<div>
<button onClick={this.openModal}>Open Modal</button>
{this.state.showModal ? <Modal /> : null}
</div>
);
}
}
In the code above, we added an openModal method that sets the state of showModal to true. We also added an onClick event to the button that calls the openModal method when the button is clicked. Finally, we used a ternary operator to check the state of showModal and display the Modal component if showModal is true.
Step 5: Closing the Modal
The final step is to add a way to close the modal. To do this, we'll add a close button to the Modal component, and when the close button is clicked, we'll set the state of showModal to false.
class Modal extends React.Component {
render() {
return (
<div>
<h1>This is a modal!</h1>
<button onClick={this.props.closeModal}>Close</button>
</div>
);
}
}
class App extends React.Component {
constructor() {
super();
this.state = {
showModal: false
};
}
openModal = () => {
this.setState({ showModal: true });
}
closeModal = () => {
this.setState({ showModal: false });
}
render() {
return (
<div>
<button onClick={this.openModal}>Open Modal</button>
{this.state.showModal ? <Modal closeModal={this.closeModal} /> : null}
</div>
);
}
}
In the code above, we added a closeModal method to the App component that sets the state of showModal to false. We also added a close button to the Modal component, and we passed the closeModal method as a prop to the Modal component.
And there you have it! You've just created a modal in ReactJS. Hope this was a fun and informative journey. Happy coding!