๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ Cross Platform/React Native

[React Native] ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ props ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ๊ธฐ(Functional Component update on props change)

by Fomagran ๐Ÿ’ป 2022. 7. 26.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค.

 

์˜ค๋Š˜์€ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ props๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ํ™”๋ฉด์ด ์—…๋ฐ์ดํŠธ ๋˜๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Problem

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ฑฐ๋‚˜ ์ˆ˜์ • ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฌ๋ฉด ํผ ํ˜•์‹์˜ ๋ชจ๋‹ฌ์ด ๋œจ๋„๋ก ๊ตฌํ˜„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

 

์œ„ ๊ณผ์ •์—์„œ ์ˆ˜์ • ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋• ํ•ด๋‹น ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฐ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์˜ ๊ฐ’์ด ์ธํ’‹ ์•ˆ์— ๋ฏธ๋ฆฌ ์ž‘์„ฑ๋ผ๋„๋ก ํ•˜๊ณ , ์ถ”๊ฐ€ ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ธ์„ ๋• ๋นˆ ์ธํ’‹์ฐฝ์ด ๋ณด์ด๋„๋ก ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.

 

์™ผ์ชฝ์ด ์ถ”๊ฐ€(+) ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ, ์˜ค๋ฅธ์ชฝ์ด ์ˆ˜์ •๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ์ž…๋‹ˆ๋‹ค.

 


TodoList.tsx

 

์œ„ ๋ฆฌ์ŠคํŠธ ํ™”๋ฉด์—์„œ useState๋ฅผ ๊ตฌํ˜„ํ•ด ์ค๋‹ˆ๋‹ค.

 

  const [todos, setTodos] = useState(todoDatas);
  const [todo, setTodo] = useState<TodoModel>({
    author: '',
    title: '',
    content: '',
    priority: -1,
  });
  const [editIndex, setEditIndex] = useState(-1);

 

์ˆ˜์ • ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์•Œ๋ฆผ์ฐฝ์ด ๋œจ๋„๋ก ๊ตฌํ˜„ํ•˜๊ณ ,

 

 

OK ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฌ๋ฉด ์ˆ˜์ • ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฐ ๋ฆฌ์ŠคํŠธ์˜ ์•„์ดํ…œ์˜ ์ธ๋ฑ์Šค๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒํƒœ๋ฅผ ์„ธํŒ…ํ•ด ์คฌ์Šต๋‹ˆ๋‹ค.

 

  const showEditAlert = (index: number) => {
    Alert.alert(
      'Edit',
      'Do you want to edit?',
      [
        {
          text: 'Cancel',
          onPress: () => console.log('Cancel Pressed!'),
        },
        {
          text: 'OK',
          onPress: () => {
            setEditIndex(index);
            setTodo(todos[index]);
            setModalVisible(true);
          },
        },
      ],
      {cancelable: false},
    );
  };

 

๋งŒ์•ฝ ์ถ”๊ฐ€(+) ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ธ๋‹ค๋ฉด ๋นˆ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „๋‹ฌ ๋˜๋„๋ก ๊ตฌํ˜„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

  const handlePlus = () => {
    setTodo({author: '', title: '', content: '', priority: -1});
    setModalVisible(true);
  };

 

๊ทธ ๋‹ค์Œ ์ปค์Šคํ…€ ๋ชจ๋‹ฌ์ธ AddModal์— ํ”„๋กœํผํ‹ฐ์— todo๋กœ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ „๋‹ฌํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

  return (
    <View>
      ...
      <AddModal
        isModalVisible={isModalVisible}
        handleClose={handleClose}
        handleSubmit={handleSubmit}
        todo={todo}></AddModal>
      ...

 

AddModal.tsx

 

ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ํ•ด ์ค๋‹ˆ๋‹ค.

 

interface AddModalProps {
  isModalVisible: boolean;
  todo: TodoModel;
  handleClose: () => {};
  handleSubmit: () => {};
}

 

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ํ”„๋กœํผํ‹ฐ๋กœ AddModalProps๋กœ ์ •์˜ํ•ด ์ฃผ๊ณ , todo ๋ชจ๋ธ์„ ์ €์žฅํ•  useState๋ฅผ ๋งŒ๋“ค๊ณ 

 

useEffect๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ props๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค todo๋ฅผ props์˜ todo๋กœ ๋ฐ”๋€Œ๋„๋ก ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

const AddModal: React.FC<AddModalProps> = props => {
  const [todo, setTodo] = useState<TodoModel>({
    author: '',
    title: '',
    content: '',
    priority: -1,
  });

  useEffect(() => {
    setTodo(props.todo);
  }, [props]);

 

๊ทธ ๋‹ค์Œ ๋ชจ๋‹ฌ ์•ˆ์˜ TextInput์— value๋กœ todo์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋„ฃ์œผ๋ฉด ์ˆ˜์ • ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฌ๋ฉด ํ•ด๋‹น ๋ชจ๋ธ์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ธํ’‹ ์ฐฝ ์•ˆ์— ์ž…๋ ฅ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

          <TextInput
            value={todo.author}
            onChangeText={text => {
              handleInputChange('author', text);
            }}
            style={styles.input}
            placeholder="author"></TextInput>
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€