๋ค์ ์ฝ๋์ ๋ฌธ์ ๊ฐ ๋ฌด์์ผ๊น?
{relData?.totalElements > 1 && (
<Button
variant='secondary'
className='px-4'
onClick={() => handleChangeRepPopup(!showChangeRepPop)}
style={{ marginRight: '10px' }}
>
{t('button.filecomponent.changeRep')}
</Button>
)}
→ 'relData.totalElements' is possibly 'undefined'.
์ฝ์์ ์ด๋ฐ ์ค๋ฅ๊ฐ ์ถ๋ ฅ ๋๋ค.
๋ง ๊ทธ๋๋ก, relData.totalElements
๊ฐ undefined ์ผ ์ ์๊ธฐ ๋๋ฌธ์ด ์ด๋ฐ ์ค๋ฅ๊ฐ ๋๋ ๊ฒ์ด๋ค.
๊ฐ์ฅ ๊ฐ๋จํ ํด๊ฒฐ๋ฐฉ๋ฒ์
relData && relData.totalElements > 1 && (
์ด๋ ๊ฒ ์กฐ๊ฑด๋ถ ๋ ๋๋ง์ ์ด์ฉํ๋ ๊ฒ์ด๋ค.
relData๊ฐ ์กด์ฌํ ๋๋ง ๋ค์ ์กฐ๊ฑด๋ฌธ๊ณผ ํจ๊ป ์๋ํ๊ณ ๋ ๋๋ง ๋๊ธฐ ๋๋ฌธ์,
relData.totalElements ๊ฐ undefined ์ผ ๊ฐ๋ฅ์ฑ์ ์์จ ์ ์๋ค.
์ด์ธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ๊ถ๊ธํ๋ค๋ฉด ์๋์ ํฌ์คํ ์ ์ฐธ๊ณ ํ๊ธธ ๋ฐ๋๋ค.
https://linguinecode.com/post/how-to-solve-typescript-possibly-undefined-value
How to solve TypeScript possibly undefined value
This article goes over 5 methods to solve a problem like TS2532: Object is possibly ‘undefined’. You can use IF conditions, logical operator, non-null assertion and more.
linguinecode.com
TypeScript possibly undefined value ํด๊ฒฐ ํ๋ ๋ฒ(+๋ฒ์ญ)
์ ๋ฌธ ๋ฒ์ญ : https://linguinecode.com/post/how-to-solve-typescript-possibly-undefined-value object์ ์ ๊ทผํด์ ์๋ง ์๋ ์๋ฌ๋ฅผ ๋ณด์ จ์ ๊ฒ๋๋ค ๋๋ ๋น์ ์ ๋ณ์ ํ๋๋ฅผ ํจ์๋ด์์ ์ ๋ฌํ๊ธธ ์ํฉ๋๋ค.
velog.io