Passing data between two sibling React.js components

前端 未结 2 1039
时光取名叫无心
时光取名叫无心 2020-12-30 07:03

I have two instances of a component (a search field) on a page, with a second component (a button that makes server calls) between them, as such:

2条回答
  •  被撕碎了的回忆
    2020-12-30 07:08

    As of 2020, Feb; Context API is the way to handle this:

    // First you need to create the TodoContext
    
    
    // Todo.jsx
    //...
    export default () => {
      return(
        <>
          
            
            
          
        
      )
    }
    
    // Now in your TodoList.jsx and TodoCalendar.jsx; you can access the TodoContext with:
    //...
    const todoContext = React.useContext(TodoContext);
    console.log(todoContext)
    //...
    //...
    
    

    Check this video tutorial by The Net Ninja for Hooks & Context API

    Good Luck...

提交回复
热议问题