locked
help with redux issue RRS feed

  • Question

  • User1034446946 posted

    Hi

    I have my state in redux, and i have a recuring function that at the start of every recurrance needs to look up an state object, everything works well

    except to keep within the hooks rules, i have to load the whole state into the component which has the recurring function using const state = useSelector(state => state)

    then in the loop use const someVariable = state['someValue']

    this works at the moment without issue, but i am concerned that as my appication grows so will the state, and i am concerned this is a bad idea as loading in a massive state could cause performance issues.

    a few questions, is this a legit concern? or am i being overly causous?

    one option would be to have all the objects related to this inside a base object mean i could only refence this part of state,however i don't think i can do this as they have seperate reducers and therefore have different state objects

    the other is find a better was to select inside the the loop, but i don't know how to do that either, i know i can't use const selector = useSelector, because that throws the rules of hooks error

    the state is used by multiple hooks, but the state isn't going to change without some user action

    any thougths would be appriciated

    Friday, October 11, 2019 3:04 AM

Answers

  • User-474980206 posted

    typically with redux there is only one state instance. so it can be passed around with no cost. normally you would use mapStateToProps via connect() to map the part of state your component is interested in.

    redux hooks are an alternative to this, the useSelector() does the mapping. The caller of the component should supply this hook via a prop, or the component is too tightly coupled to the state.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 11, 2019 8:03 PM

All replies

  • User-474980206 posted

    typically with redux there is only one state instance. so it can be passed around with no cost. normally you would use mapStateToProps via connect() to map the part of state your component is interested in.

    redux hooks are an alternative to this, the useSelector() does the mapping. The caller of the component should supply this hook via a prop, or the component is too tightly coupled to the state.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 11, 2019 8:03 PM
  • User1034446946 posted

    thnks for the info

    Saturday, October 12, 2019 1:45 PM