应该在React状态中混合变量和常量数据吗?

huangapple go评论67阅读模式
英文:

Should I mix variable and constant data inside a react state?

问题

I have some data that will be defined inside my code (without fetching) and won't change during the lifecycle of my app. Let's say that this data is a list of objects representing streets, that have properties such as name, size etc that are permanent.

But related to each street there are, for example, number of houses inside it, and it might change.

Is it better to use number of houses as a property and keep the entire streets list inside the app state, or have only a list of number of houses in the app state and associate them with streets based on indexes/keys?

Sorry, I couldn't figure out a better generical example. Thanks

英文:

I'm trying to think about the data structure of my app and came into an interesting question:

I have some data that will be defined inside my code (without fetching) and won't change during the lifecycle of my app. Let's say that this data is a list of objects representing streets, that have properties such as name, size etc that are permanent.

But related to each street there are, for example, number of houses inside it, and it might change.

Is it better to use number of houses as a property and keep the entire streets list inside the app state, or have only a list of number of houses in the app state and associate them with streets based on indexes/keys?

Sorry, I couldn't figure out a better generical example. Thanks

答案1

得分: 1

我认为对于您的情况,最佳方法是使用**useReducer钩子,它非常适用于数组/对象格式的状态。因此,对于streets 常量,您可以将其声明在一个单独的文件中(例如 consts.js),以便在需要时在多个组件中重用,然后将其导入到您想要处理房屋数量**的组件中:

import { STREETS } from "consts";

const Actions = {
  ADD_HOUSE: 'ADD_HOUSE',
  REMOVE_HOUSE: 'REMOVE_HOUSE',
  UPDATE_HOUSES: 'UPDATE_HOUSES',
  RESET: 'RESET'
};

const initialState = { 
    streets: STREETS
}

function reducer(state, {type, payload}) {
    switch(type){
      case Actions.ADD_HOUSE:{
        //您的逻辑
      }
      //其他情况...
    }
}

function YourComponent() {
  const [{ streets }, dispatch] = useReducer(reducer, initialState);
  //其他代码...
}

注意:最好将 reducer 逻辑与您的组件分开,您可以使用另一个文件,例如reducer.js,然后导出Actions、initialState 和 reducer 函数

英文:

I think the best approach for your case is to use useReducer hook which is efficient for states in format of Array/Object. So for the streets constant you can declare it in a separate file (consts.js for example) so you can reuse it in multiple components if needed then import it to your component where you want to handle the number of houses:

import { STREETS } from "consts";


const Actions = {
  ADD_HOUSE: 'ADD_HOUSE',
  REMOVE_HOUSE: 'REMOVE_HOUSE',
  UPDATE_HOUSES: 'UPDATE_HOUSES'
  RESET: 'RESET'
};
  
const initialState = { 
    streets: STREETS
}
  
function reducer(state, {type, payload}) {
    switch(type){
      case Actions.ADD_HOUSE:{
        //You logic
      }
      ...
    }
}
function YourComponent() {
  const [{ streets }, dispatch] = useReducer(reducer, initialState);
...
}

NOTE: It's better to separate the reducer logic from your component you can use another file called reducer.js for example then export Actions, initialState, reducer func.

huangapple
  • 本文由 发表于 2023年4月4日 05:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定