반응형

react-native-calendars 사용 중에 오늘 날짜로 이동하는 기능을 구현하려 했다.
'오늘로' 버튼을 누를 때마다 todayString이라는 오늘 날짜가 업데이트 되도록 했다.
Calendar 컴포넌트에서 current props에 state로 관리 중인 todayString를 넣어봤지만, 달력 이동이 안됐다.
const [todayString, setTodayString] = useState('');
return (
<Calendar
current={todayString}
key={todayString}
/>
);
이 문제를 해결하기 위해서 key prop을 위한 state를 하나 더 만들었다. current prop 대신에 key prop에 변화되는 값을 주어, '오늘로' 버튼을 누를 때마다 오늘 날짜로 캘린더가 리렌더링 되도록 하였다.
const [todayString, setTodayString] = useState('');
const [calendarKey, setCalendarKey] = useState(0);
return (
<View>
<TouchableOpacity
onPress={() => { setCalendarKey(Math.random()); }}
>
<Text>오늘로</Text>
</TouchableOpacity>
<Calendar
current={todayString}
key={calendarKey}
/>
</View>
);반응형
'ReactNative' 카테고리의 다른 글
| [React Native] 실제 기기(아이폰) 연결해서 프로젝트 구동하기 (2) | 2024.07.05 |
|---|---|
| [React Native] fetch 사용 시 TypeError: Network request failed (0) | 2024.07.01 |
| [React Native] 탭 간 이동시 탭에 쌓여있는 스택 새로고침/초기화하기 (0) | 2024.06.11 |
| [React Native] 시뮬레이터 기종 지정하기 (0) | 2024.06.11 |
| [React Native]error Failed to build iOS project. "xcodebuild" exited with error code '65'. (0) | 2024.05.16 |
