If Else Statement in ReactJS
Today we will show you how to use if else statement in ReactJS. It’s a basic tutorial for react beginners. You can use If-Else statement while you need to render HTML conditionally.
If Else Statement in ReactJS, Conditional Rendering in React JS, How to Write if/else Conditional Statements in JSX, react if statement in map, react if statement in map, react conditional render multiple elements, react: use a ternary expression for conditional rendering, react: render conditionally from props, jsx control statements.
Checkout more articles on ReactJS
Example:
Let’s take an example to understand the if else statement for conditional rendering. We will display the counter on screen and use the edit button to update the counter. Once you click on the edit button then we will change the layout and display the update screen where you can update the counter and save it.
Screen 1:
The below screen will be displayed when you click on the “Edit” button.
Screen 2:
We will be redirected back to Screen 1 on the click of the “Update” button.
We can split this example in two parts and manage it by if else statement.
1. Display section
In this section, we will display the counter text and handle the edit button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import React, { useState } from 'react'; function App() { const [counter, setCounter] = useState(0); const [editMode, setEditMode] = useState(false); const handleEditClick = () => { setEditMode(true); } if (editMode) { // Edit section return <div> <a href="https://www.cluemediator.com">Clue Mediator</a><br /><br /> Edit Section </div> } else { // Display section return ( <div> <a href="https://www.cluemediator.com">Clue Mediator</a><br /><br /> <div>Counter: {counter}</div><br /> <input type="button" value="Edit" onClick={handleEditClick} /> </div> ); } } export default App; |
2. Edit section
For this screen, we will have a counter text, input field to update the counter and use button to update the value of counter on click of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import React, { useState } from 'react'; function App() { const [counter, setCounter] = useState(0); const [editMode, setEditMode] = useState(false); const [input, setInput] = useState(counter); const handleEditClick = () => { setInput(counter); setEditMode(true); } const handleUpdateClick = () => { setCounter(input); setEditMode(false); }; if (editMode) { // Edit section return <div> <a href="https://www.cluemediator.com">Clue Mediator</a><br /><br /> <div>Counter: {counter}</div><br /> <input type="number" value={input} onChange={e => setInput(e.target.value)} /> <input type="button" value="Update" onClick={handleUpdateClick} /> </div> } else { // Display section return ( <div> <a href="https://www.cluemediator.com">Clue Mediator</a><br /><br /> <div>Counter: {counter}</div><br /> <input type="button" value="Edit" onClick={handleEditClick} /> </div> ); } } export default App; |
Output
Thank you for reading. Happy Coding!