• traveler@lemdro.id
    link
    fedilink
    English
    arrow-up
    2
    arrow-down
    2
    ·
    1 year ago

    You can pass data to top components from the child ones using the useContext. Use it only if you have data where you want to pass data from multiple child components to the parent one, if you just want to use from one component you are good with states.

    • zorlan@aussie.zone
      link
      fedilink
      arrow-up
      5
      ·
      edit-2
      1 year ago

      I think you’re being down voted because context should be used sparingly. You can pass data back up the parent chain through prop functions.

      • traveler@lemdro.id
        link
        fedilink
        English
        arrow-up
        2
        arrow-down
        1
        ·
        1 year ago

        Yes I’m aware, was just giving a suggestion. I even linked to its documentation where all that information is available.

        To be fair couldn’t care less if Im voted down or not, if I could I’d have an option to just hide that vote bullshit it’s just visual garbage in the UI. Basically a remnant from centralised social networks from the web2 era.

    • DrQuint@lemm.ee
      link
      fedilink
      arrow-up
      5
      arrow-down
      1
      ·
      edit-2
      1 year ago

      If there’s a parent component that has some data that it expects to always receive from its children, then that data should be in the parent’s state and the children should receive it and maybe some relevant methods as props. Even if it’s an unknown number of children. Don’t muck with useContext for basic inheritance stuff, you’ll mess with the render cycle for no good reason.

      Now, if we’re talking about a very distant “top” component, that’s fine, it’s what it was made for. Although many people end up using stores if it’s some data with broad impact (like user data)