만약 WriteHeaderuseReducer로 구현한다면 어떤 모습일지 한번 코드를 확인해볼까요?

    components/WriteHeader.js

    import {useNavigation} from '@react-navigation/native';
    import {format} from 'date-fns';
    import {ko} from 'date-fns/locale';
    import React, {useReducer} from 'react';
    import {Pressable, StyleSheet, Text, View} from 'react-native';
    import TransparentCircleButton from './TransparentCircleButton';
    import DateTimePickerModal from 'react-native-modal-datetime-picker';
    
    const initialState = {mode: 'date', visible: false};
    function reducer(state, action) {
      switch (action.type) {
        case 'open':
          return {
            mode: action.mode,
            visible: true,
          };
        case 'close':
          return {
            ...state,
            visible: false,
          };
        default:
          throw new Error('Unhandled action type');
      }
    }
    
    function WriteHeader({onSave, onAskRemove, isEditing, date, onChangeDate}) {
      const navigation = useNavigation();
      const onGoBack = () => {
        navigation.pop();
      };
    
      const [state, dispatch] = useReducer(reducer, initialState);
      const open = (mode) => dispatch({type: 'open', mode});
      const close = () => dispatch({type: 'close'});
    
      const onConfirm = (selectedDate) => {
        close();
        onChangeDate(selectedDate);
      };
    
      return (
        <View style={styles.block}>
          <View style={styles.iconButtonWrapper}>
            <TransparentCircleButton
              onPress={onGoBack}
              name="arrow-back"
              color="#424242"
            />
          </View>
          <View style={styles.buttons}>
            {isEditing && (
              <TransparentCircleButton
                name="delete-forever"
                color="#ef5350"
                hasMarginRight
                onPress={onAskRemove}
              />
            )}
            <TransparentCircleButton
              name="check"
              color="#009688"
              onPress={onSave}
            />
          </View>
          <View style={styles.center}>
            <Pressable onPress={() => open('date')}>
              <Text>
                {format(new Date(date), 'PPP', {
                  locale: ko,
                })}
              </Text>
            </Pressable>
            <View style={styles.separator} />
            <Pressable onPress={() => open('time')}>
              <Text>{format(new Date(date), 'p', {locale: ko})}</Text>
            </Pressable>
          </View>
          <DateTimePickerModal
            isVisible={state.visible}
            mode={state.mode}
            onConfirm={onConfirm}
            onCancel={close}
            date={date}
          />
        </View>
      );
    }
    
    (...)

    이 코드만 봤을 때는 useReducer를 사용함으로써 오히려 조금 복잡해졌다고 느낄 수도 있을 텐데요. 일단 이러한 Hook이 있다는 것만 잘 알아두세요. 앞으로 상태를 관리할 때 기본적으로는 useState를 사용해 구현하고, 함께 바뀌는 상태 값들이 많아졌을 때는 useReducer를 사용할지 고민해보면 좋습니다.

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.