https://react.dev/learn/choosing-the-state-structure

๋ฆฌ์•กํŠธ์—์„œ ์ƒํƒœ ๊ตฌ์กฐ๋ฅผ ์งค ๋•Œ์˜ ์›์น™๋“ค์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•˜๋Š” ์„น์…˜์ด๋‹ค. DB์˜ ์ •๊ทœํ™”์ฒ˜๋Ÿผ ์‹ค์ˆ˜๋‚˜ ๋ฒ„๊ทธ๊ฐ€ ๋ฐœ์ƒํ•  ๋ถ€๋ถ„์„ ์ค„์ด๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋‹ค.

  • Group related state
// BAD
const [x, setX] = useState(0);
const [y, setY] = useState(0);
 
// GOOD
const [position, setPosition] = useState({x:0, y:0});
  • Avoid contradictions in state
// BAD
const [isSending, setIsSending] = useState(false);
const [isSent, setIsSent] = useState(false);
 
// GOOD
const [status, setStatus] = useState("typing");
 
const isSending = status === "sending";
const isSent = status === "sent";
- BAD์—์„œ๋Š” `isSending`๊ณผ `isSent`๋ผ๋Š” ์ƒ๋ฐ˜๋œ ์ƒํƒœ๋ฅผ ๋‘์—ˆ๋‹ค. ๋™์ž‘์€ ํ•˜๊ฒ ์ง€๋งŒ, ์‹ค์ˆ˜๋กœ ํ•œ๊ฐ€์ง€ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. 
- ๋”ฐ๋ผ์„œ GOOD์—์„œ์™€ ๊ฐ™์ด status๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋‚˜์˜ ์ƒํƒœ๋ฅผ ๋งŒ๋“ค๊ณ , isSending๊ณผ isSent๋Š” ๊ทธ status์— ๋”ฐ๋ผ ๊ฐ’์ด ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ ๋˜๋„๋กํ•˜์˜€๋‹ค.
  • Avoid redundant in state : props๋‚˜ ๋‹ค๋ฅธ ์ƒํƒœ๋“ค๋กœ๋ถ€ํ„ฐ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅํ•œ ๊ฐ’์€ ์ƒํƒœ๋กœ ์ง€์ •ํ•˜์ง€ ๋ง๋ผ
// BAD
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [fullName, setFullName] = useState("");
 
// GOOD
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
 
const fullName = firstName + ' ' + lastName;
  • Avoid duplication in state
// BAD
const [items, setItems] = useState(initialItems);
const [selectedItem, setSelectedItem] = useState(initialItems[0]);
 
 
// GOOD
const [items, setItems] = useState(initialItems);
const [selectedId, setSelectedId] = useState(0);
- BAD์—์„œ๋Š” items์— ๋‹ด๊ธด ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ฅผ selectedItem์—๋„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋‘๊ฐœ๋‹ค ์—…๋ฐ์ดํŠธ ํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์—์„œ ์‹ค์ˆ˜๋กœ ํ•˜๋‚˜๋ฅผ ๋บด๊ณ  ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.
- ๋”ฐ๋ผ์„œ GOOD์—์„œ์ฒ˜๋Ÿผ ์ค‘๋ณต๋œ ์ •๋ณด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์ด ๋ฐ”๋žŒ์งํ•˜๋‹ค.
  • Avoid deeply nested state
// BAD
export const initialTravelPlan = {
  id: 0,
  title: '(Root)',
  childPlaces: [{
    id: 1,
    title: 'Earth',
    childPlaces: [{
      id: 2,
      title: 'Africa',
      childPlaces: [{
        id: 3,
        title: 'Botswana',
        childPlaces: []
      }, {
        id: 4,
        title: 'Egypt',
        childPlaces: []
      }, {
        id: 5,
        title: 'Kenya',
        childPlaces: []
      }, {
        id: 6,
        title: 'Madagascar',
        childPlaces: []
      }, {
        id: 7,
        title: 'Morocco',
        childPlaces: []
      }, {
        id: 8,
        title: 'Nigeria',
        childPlaces: []
      }, {
        id: 9,
        title: 'South Africa',
        childPlaces: []
      }]
    }]
  }]
};
 
// GOOD
export const initialTravelPlan = {
  0: {
    id: 0,
    title: '(Root)',
    childIds: [1],
  },
  1: {
    id: 1,
    title: 'Earth',
    childIds: [2, 10, 19, 26, 34]
  },
  2: {
    id: 2,
    title: 'Africa',
    childIds: [3, 4, 5, 6 , 7, 8, 9]
  }, 
  3: {
    id: 3,
    title: 'Botswana',
    childIds: []
  },
  4: {
    id: 4,
    title: 'Egypt',
    childIds: []
  },
  5: {
    id: 5,
    title: 'Kenya',
    childIds: []
  },
  6: {
    id: 6,
    title: 'Madagascar',
    childIds: []
  }, 
  7: {
    id: 7,
    title: 'Morocco',
    childIds: []
  },
  8: {
    id: 8,
    title: 'Nigeria',
    childIds: []
  },
  9: {
    id: 9,
    title: 'South Africa',
    childIds: []
  },
}
 
	- ๋„ˆ๋ฌด ๊นŠ์€ object๋ฅผ ์ƒํƒœ์— ๋„ฃ์œผ๋ฉด ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ• ๋•Œ ์ฝ”๋“œ๊ฐ€ ์žฅํ™ฉํ•ด์งˆ ์ˆ˜ ์žˆ๋‹ค.
	- ์ถ”์ฒœํ•˜๋Š” ๋ฐฉ๋ฒ•์€ nested object๋ฅผ flatํ•˜๊ฒŒ ๋ณ€๊ฒฝํ•˜์—ฌ, ๊ฐ์ž ์ž๊ธฐ ์ž์‹์˜ id๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์‹์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

review