250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- typeScript
- SSR
- HTML
- built in object
- nextJS
- Ethereum
- Redux
- evm
- blockchain
- CLASS
- graphQL
- middleware
- REACT
- Props
- JavaScript
- useState
- API
- CSS
- bitcoin
- hardhat
- solidity
- error
- tailwindcss
- 삶
- web
- node.js
- express.js
- 기준
- Interface
- concept
Archives
- Today
- Total
ReasonJun
React : Conditional render 본문
728x90
Conditional rendering in React refers to the ability to display different components or content based on certain conditions or logical expressions. It allows you to control what gets rendered to the DOM based on the state or props of your components.
There are several ways to implement conditional rendering in React:
- if statement: You can use a standard JavaScript if statement to conditionally render components. For example:
function MyComponent(props) {
if (props.isLoggedIn) {
return <LoggedInComponent />;
} else {
return <LoggedOutComponent />;
}
}
- Ternary operator: The ternary operator (condition ? expression1 : expression2) is a concise way to conditionally render components. For example:
function MyComponent(props) {
return (
<div>
{props.isLoggedIn ? <LoggedInComponent /> : <LoggedOutComponent />}
</div>
);
}
- Logical && operator: The && operator can be used to conditionally render a component based on a condition. It is useful when you want to render a component only if a certain condition is true. For example:
function MyComponent(props) {
return (
<div>
{props.isLoaded && <DataComponent />}
</div>
);
}
- Switch statement: If you have multiple conditions to check, you can use a switch statement for more complex conditional rendering. For example:
function MyComponent(props) {
switch (props.status) {
case 'loading':
return <LoadingComponent />;
case 'success':
return <SuccessComponent />;
case 'error':
return <ErrorComponent />;
default:
return <DefaultComponent />;
}
}
728x90
'Frontend > React' 카테고리의 다른 글
React : HOCs (Higher-Order Components) (0) | 2023.06.12 |
---|---|
React : Hooks (0) | 2023.06.12 |
React : Function and Class Components / Rendering a Component / Composing Components / Extracting Components / Props are Read-Only (0) | 2023.06.11 |
React : Passing values to components through props (0) | 2023.06.11 |
React : Pros and Cons of SPA (2) | 2023.06.11 |
Comments