React is updating both child component when data linked to one child is changing

Rajan Lagah

I have 2 component

  1. Shape
  2. video

And they are in 1 route ./home

And i have context for ./home route as

<Router>
  <div >
    <PContext>
      <Route exact path="/" component={home} />
    </PContext>
  </div>
</Router>

Now in home i have

...
function Home() {
  
    useEffect( ()=>{
        console.log('Rendered PDetect')
    })
    
    return (
        <div>
                <Video/>
                <Shape/>
        </div>
    );
}

export default Home;

And i have function in Video child ( to detect aspect ratio ) and it will update the value of aspect ratio in variable in PContext and shape component will use that value and display it.

...
const Shape = () => {

    const { ratio }  = useContext(PDataContext)

    console.log("ratio",ratio)
    useEffect( ()=>{
        console.log('Rendered Shape')
    })
      
    return (
       <span className="shape_circle" >1</span>
    )
}

export default Shape

And context file is

...  
export const PDataContext = createContext()

const PContext = ( props ) => {

   const [ ratio , setSetRatio ] = useState([])
  
  const gotRatio = r => {
    setSetRatio(r[0].key)
  }

  return(<PoseDataContext.Provider value={{
            gotRatio,
            ratio}}>
                {props.children}
        </PoseDataContext.Provider>
  )
}

export default PoseContext

My problem is that Video child use gotRatio function and update the value of ratio and shape component use ratio value to display it. But when gotRatio function update it re-render both Shape and Video component so Video start from 0sec again

aeXuser264

All the consumers will update its expected as of ReactJS Doc

All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes. The propagation from Provider to its descendant consumers (including .contextType and useContext) is not subject to the shouldComponentUpdate method, so the consumer is updated even when an ancestor component skips an update.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Child component not updating in React

From Dev

React child component not updating when props change

From Dev

How to hide a child component when changing React route

From Dev

Meteor react state passed to child component not updating

From Dev

React - Updating state for each child component

From Dev

Updating state with props on React child component

From Dev

Updating Parent Component via Child Component in React Native

From Dev

Updating a parent component when child component variable is changed

From Dev

How to reload the Data of a child when a child Component is being rendered in ReactJs

From Java

React Child Component Not Updating After Parent State Change

From Dev

react child component not updating after parent state changed

From Dev

How to get useForm data in react component when input fields are in child component?

From Dev

Parent state not updating child component

From Dev

React: Calling two event handlers onClick and changing style of child component

From Dev

Parent variable not updated when updating trough child component

From Dev

How to access data in a child react component from a parent react component

From Dev

Child Component Not Binding in React

From Dev

React: How do I fix getting undefined when passing api data to child component using axios?

From Dev

How does one trigger an action in a child functional component in React?

From Dev

How does one trigger an action in a child functional component in React?

From Dev

List of Child model not updating when parent is updating

From Dev

Redux child not updating parent component state

From Dev

ReactJs - Redux State is not updating in a dropdown in the child component

From Dev

Child Component Isn't Updating with New Props

From Dev

React not re-render when child component updated

From Dev

React update parent component when child changes the Context

From Dev

React Jest to match snapshot, crash when testing component with child components

From Dev

react child component fails to render when calling parent callback with setstate

From Dev

Trigger event when the state of child of another component changed in react

Related Related

  1. 1

    Child component not updating in React

  2. 2

    React child component not updating when props change

  3. 3

    How to hide a child component when changing React route

  4. 4

    Meteor react state passed to child component not updating

  5. 5

    React - Updating state for each child component

  6. 6

    Updating state with props on React child component

  7. 7

    Updating Parent Component via Child Component in React Native

  8. 8

    Updating a parent component when child component variable is changed

  9. 9

    How to reload the Data of a child when a child Component is being rendered in ReactJs

  10. 10

    React Child Component Not Updating After Parent State Change

  11. 11

    react child component not updating after parent state changed

  12. 12

    How to get useForm data in react component when input fields are in child component?

  13. 13

    Parent state not updating child component

  14. 14

    React: Calling two event handlers onClick and changing style of child component

  15. 15

    Parent variable not updated when updating trough child component

  16. 16

    How to access data in a child react component from a parent react component

  17. 17

    Child Component Not Binding in React

  18. 18

    React: How do I fix getting undefined when passing api data to child component using axios?

  19. 19

    How does one trigger an action in a child functional component in React?

  20. 20

    How does one trigger an action in a child functional component in React?

  21. 21

    List of Child model not updating when parent is updating

  22. 22

    Redux child not updating parent component state

  23. 23

    ReactJs - Redux State is not updating in a dropdown in the child component

  24. 24

    Child Component Isn't Updating with New Props

  25. 25

    React not re-render when child component updated

  26. 26

    React update parent component when child changes the Context

  27. 27

    React Jest to match snapshot, crash when testing component with child components

  28. 28

    react child component fails to render when calling parent callback with setstate

  29. 29

    Trigger event when the state of child of another component changed in react

HotTag

Archive