Conditional Rendering in REACT
Conditional Rendering in REACT
```jsx
function Greeting(props) {
const isLoggedIn = props.isLoggedIn;
return (
<div>
{isLoggedIn ? (
<h1>Welcome, User!</h1>
):(
<button>Login</button>
)}
</div>
);
}
```
In this example, if `isLoggedIn` is true, it renders a welcome message. Otherwise, it renders a login
button.
```jsx
function ShowMessage(props) {
const showMessage = props.showMessage;
return (
<div>
{showMessage && <p>This is a message.</p>}
</div>
);
}
```
3. **Using a Function:**
```jsx
function UserGreeting() {
return <h1>Welcome back, User!</h1>;
}
function GuestGreeting() {
return <h1>Please sign up.</h1>;
}
function Greeting(props) {
const isLoggedIn = props.isLoggedIn;
if (isLoggedIn) {
return <UserGreeting />;
} else {
return <GuestGreeting />;
}
}
```
In this example, the `Greeting` component renders either the `UserGreeting` or `GuestGreeting`
component based on the `isLoggedIn` prop.
```jsx
function ToggleButton() {
const [isOn, setIsOn] = useState(false);
return (
<div>
{isOn ? (
<button onClick={handleClick}>Turn Off</button>
):(
<button onClick={handleClick}>Turn On</button>
)}
</div>
);
}
```
Here, clicking the button toggles the state between `isOn` and `!isOn`, which determines whether to
render "Turn On" or "Turn Off" button.
These are basic examples to illustrate the concept of conditional rendering in JSX. In more complex
applications, you can use conditional rendering to control the display of components, sections, or
entire views based on various conditions and user interactions.
A more detailed example using a Clothing Store App that includes a
1. Product Page,
2. Contact Page,
3. Header Component with a Notification Message, that receives output from a;
4. Basic Login function and a;
5. Basic Logout function, linked to a;
6. Button.
```jsx
import React, { useState } from 'react';
import Container from 'react-bootstrap/esm/Container';
import Navbar from 'react-bootstrap/Navbar';
import Button from 'react-bootstrap/Button';
import { Routes, Route, Link } from 'react-router-dom';
import logo from "./crockett-and-bond-red-gold.png";
import image from "./jb-cj-collection.jpg"
return (
<Navbar className="bg-body-tertiary">
<Container className='text-center' style={{ border: '2px solid white', backgroundColor: 'white'
}}>
<Navbar.Brand href="#home">
<img
alt=""
src={logo}
width="50"
height="50"
className="d-inline-block align-center"
/>{' '}
Crockett & Bond
</Navbar.Brand>
{/* Display login status using ternary operator */}
<p>You are {isLoggedIn ? 'logged in' : 'not logged in'}</p>
</Container>
</Navbar>
);
}
const products = [
// ... (Product data)
];
return (
<div className="App">
{/* Render the Header component with isLoggedIn prop */}
<Header isLoggedIn={isLoggedIn} />
{/* Render the Greeting component */}
<Greeting
isLoggedIn={isLoggedIn}
name="Agent"
onSignIn={() => setIsLoggedIn(true)}
onSignOut={() => setIsLoggedIn(false)}
/>
{/* Define routes using React Router */}
<Routes>
<Route exact path="/" element={<Landing />} />
{/* ... (Other routes) */}
</Routes>
{/* Render Product components */}
{products.map(product => (
<Product key={product.id} name={product.name} price={product.price}
image={product.image} />
))}
</div>
);
}
**Explanation:**
1. **Ternary Operator in `Greeting` Component:**
The `Greeting` component uses a ternary operator to conditionally render content based on
whether the user is logged in or not. The ternary operator has two parts separated by a `?` symbol.
The first part is the condition (`isLoggedIn`), followed by a `:` symbol, and then the two possible
outcomes for the true and false cases.
- If `isLoggedIn` is `true`, it renders the content for a logged-in user (including the `Welcome`
component and `SignOut` button).
- If `isLoggedIn` is `false`, it renders the content for a user who is not logged in (including the
`SignIn` button).
- `Welcome`, `SignIn`, and `SignOut` components are functional components that take props and
return JSX elements with specific styles and behavior.
- The `Greeting` component receives props like `isLoggedIn`, `name`, `onSignIn`, and
`onSignOut`, and it dynamically renders either the logged-in or not logged-in content.
- The `Header` component uses a ternary operator to display a message based on the `isLoggedIn`
prop. If the user is logged in (`isLoggedIn` is `true`), it displays "You are logged in"; otherwise, it
displays "You are not logged in".
- The `App` component manages the `isLoggedIn` state using the `useState` hook. It also defines
an array of product data.
- It passes the `isLoggedIn` state and functions (`onSignIn` and `onSignOut`) as props to child
components like `Header` and `Greeting`.
- The `Routes` component from `react-router-dom` is used to define routes for different pages in
the application.
- `Route` components define the paths and corresponding components to be rendered for those
paths.
- `Link` components are used to create navigation links between different routes.
The provided code creates a simple React application with conditional rendering based on the user's
login status. It also demonstrates how to use the ternary operator to conditionally render content and
how to link child components and functions through props.