komal deep singh chahal
komal deep singh chahal

Reputation: 1259

global variables(Communication between components in react.js

Is it good idea to use global variables for communication between 2 components in react.js. Or is it bad practice.

If no, can somebody explains, whats are the drawbacks of it.

Thanks

Upvotes: 1

Views: 968

Answers (2)

Jackson
Jackson

Reputation: 139

One of the core value propositions of React is one way data flow. Among other benefits, it makes it easy for developers to reason about as they trace the data flow. You don't want child-child communication, instead I would suggest structuring your app such that the parent can pass data to both children.

Upvotes: 1

1ven
1ven

Reputation: 7016

Any global variables in most cases is bad practice in React applications.

If you have some data, which should be used by many components, consider using some Flux library like Redux.

Upvotes: 1

Related Questions