How to make a shared state between two react components?

前端 未结 5 511
盖世英雄少女心
盖世英雄少女心 2020-11-27 07:04

I have 2 react components that need to share a state, react-router shows component A, which takes some inputs and adds it to its state, after the state has been successfully

5条回答
  •  心在旅途
    2020-11-27 07:12

    The dependency type between the components will define the best approach.

    For instance, redux is a great option if you plan to have a central store. However other approaches are possible:

    • Parent to Child

      1. Props
      2. Instance Methods
    • Child to Parent

      1. Callback Functions
      2. Event Bubbling
    • Sibling to Sibling

      1. Parent Component
    • Any to Any

      1. Observer Pattern
      2. Global Variables
      3. Context

    Please find more detailed information about each of the approaches here

提交回复
热议问题