728x90
๐ React Portal
- ์ปดํฌ๋ํธ๋ฅผ DOM Tree์ ๋ค๋ฅธ ์์น์ ๋ ๋๋งํ ์ ์๊ฒ ํ๋ ๊ธฐ๋ฅ.
- ๋ชจ๋ฌ, ๋ ์ด์ด ํ์ ์์ ๋ง์ด ์ฌ์ฉํจ.

import { useState } from 'react';
import { createRoot } from 'react-dom/client';
import { createPortal } from 'react-dom';
function Modal( {children} ) {
const style = {
position: 'fixed',
top : 0,
left : 0,
right : 0,
bottom : 0,
backgroundColor : 'rgba(0,0,0,0.5)',
display : 'flex',
alignItems : 'center',
justifyContent : 'center'
}
const style2 = {
backgroundColor: 'white',
padding : '20px',
borderRadius : '20px'
}
return createPortal(
<div style={style}>
<div style={style2}>{children}</div>
</div>,
document.body
);
}
function App() {
const [open, setOpen] = useState(false);
return (
<div>
<h1>React Portal Example</h1>
<button onClick={ () => setOpen(true) }> open modal </button>
{ open && (
<Modal>
<h2>์๋
ํ์ธ์</h2>
<button onClick={ () => setOpen(false) } >๋ซ๊ธฐ</button>
</Modal>
)
}
</div>
);
}
createRoot(document.getElementById('mainModal')).render(
<App />
);
๐ React Suspense
- ์ฝ๋๋ ๋ฐ์ดํฐ๊ฐ ๋ก๋๋ ๋๊น์ง ๋์ฒด HTML์ ํ์ํ๋ ๊ธฐ๋ฅ
- ์์ง ๋ก๋ฉ์ด ์๋๋ฉด,,, ๋ก๋ฉ ๋๊ธฐ ์ ๊น์ง์ LOADING... ๊ฐ์ ๊ฑธ ํ์ํด์ฃผ๊ธฐ ์ํจ.
- React.lazy๋ ๋ฐ๋์ import()์ ํจ๊ป ์จ์ผ ํ๋ค.
import { createRoot } from 'react-dom/client';
import React, { Suspense } from 'react';
// const LazyGreeting = React.lazy( () => import('./Greeting') );
const LazyGreeting = React.lazy( () =>
new Promise((resolve) => {
setTimeout( () => resolve(import('./Greeting')), 2000 );
})
);
function App() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<LazyGreeting />
</Suspense>
</div>
);
}
createRoot(document.getElementById('suspense')).render(
<App />
);
## Greating.jsx
function Greeting() {
return <h1>์๋
ํ์ธ์??</h1>
}
export default Greeting;
์ถ์ฒ :
๊ฐ๋ฐ ๊ณต๋ถ๋ฅผ ์ํ ๋ธ๋ก๊ทธ ์ ๋๋ค.
์ค๋ฅ๊ฐ ์๋ค๋ฉด ๋๊ธ๋ก ์๋ ค์ฃผ์ธ์!
๊ฐ์ฌํฉ๋๋ค.

728x90