It depends on your app's style. If it is a simple app where component two needs to access component one's variables props would be easy to use. However, as apps scale, you need to consider situations where component two needs to access a global state. Then, things change. Suppose your ComponentOne is the parent that contains & controls the state and ComponentTwo is the child and will only use the state passed from the parent.
Component1.js
import React from 'react';
import ComponentTwo from "./yourcomponent2directory.js"
const ComponentOne = () => {
varOne = Mike;
varTwo = Tyson;
return
(
<div>
<ComponentTwo varOne={varOne} varTwo={varTwo}/>
</div>
)}
export default ComponentOne
ComponentTwo.js
import React from 'react';
import ComponentOne from '../OtherFolder/';
const ComponentTwo = (props) => {
return(
<div>
<p>{props.varOne}, {props.varTwo}</p>
</div>
)}
export default ComponentTwo
or you can destructure props like...
const ComponentTwo = ({varOne,varTwo}) => {
return(
<div>
<p>{varOne}, {varTwo}</p>
</div>
)}
export default ComponentTwo
Answer from Ahmet Ulutaş on Stack OverflowHow to pass data from one component to another in react without re-render or jsx like from a function using functional components
React.js passing one components variables to another component and vice-versa
reactjs - How to send data from one component to another component in react without parent child relation - Stack Overflow
How can I pass data to a component without props in React?
How do I create a Context in React?
Can React Context replace state management libraries like Redux?
When should I use React Context?
It depends on your app's style. If it is a simple app where component two needs to access component one's variables props would be easy to use. However, as apps scale, you need to consider situations where component two needs to access a global state. Then, things change. Suppose your ComponentOne is the parent that contains & controls the state and ComponentTwo is the child and will only use the state passed from the parent.
Component1.js
import React from 'react';
import ComponentTwo from "./yourcomponent2directory.js"
const ComponentOne = () => {
varOne = Mike;
varTwo = Tyson;
return
(
<div>
<ComponentTwo varOne={varOne} varTwo={varTwo}/>
</div>
)}
export default ComponentOne
ComponentTwo.js
import React from 'react';
import ComponentOne from '../OtherFolder/';
const ComponentTwo = (props) => {
return(
<div>
<p>{props.varOne}, {props.varTwo}</p>
</div>
)}
export default ComponentTwo
or you can destructure props like...
const ComponentTwo = ({varOne,varTwo}) => {
return(
<div>
<p>{varOne}, {varTwo}</p>
</div>
)}
export default ComponentTwo
There are three different communication b/w components based on their relation.
- Passing data from Parent-to-child
For passing data from parent to child component, we use props. Props data is sent by the parent component and cannot be changed by the child component as they are read-only.
- Passing data from Child-to-parent
For passing the data from the child component to the parent component, we have to create a callback function in the parent component and then pass the callback function to the child component as a prop. This callback function will retrieve the data from the child component. The child component calls the parent callback function using props and passes the data to the parent component.
- Passing data b/w Siblings
For passing data among siblings, there are multiple methods we can choose from as shown below:
- Combination of the above two methods (callback and use of props).
- Using Redux.
- ContextAPI
#Copied from this link. You should check the given link and go through that how to pass when we have such relationship b/w components and when to use what.
The pattern that you have mentioned is children prop pattern, where the nested JSX gets passed to the component as the children.
When you include JS as part of JSX, you will have to wrap them in {}
<Form>
{ checkboxes.map(id => <Checkbox key={id} id={id} />) }
</Form>
Your Form component render method would look something like this.
render() {
<div>
{this.props.children}
</div>
}
If the children that you passed is a function, you would just invoke it in the Form component.
<Form>
{() => {
return checkboxes.map(id => <Checkbox key={id} id={id} />)
}}
</Form>
You just invoke the children cause it is passed as a function.
render() {
<div>
{this.props.children()}
</div>
}
Anything passed inside a component like that is automatically converted to the children prop. You can access them inside Form like this:
//...
render() {
<div>
{this.props.children}
</div>
}
//...
I'm new to React/Typescript with a Java/Python background. Maybe I am misunderstanding how to do this (or perhaps its some sort of anti-pattern..) but is it really hard to share state variables between components? What if I have an inputbutton component that acts as a setter for a state variable and another component that relies on the output of setter?
It seems quite complicated too create a parent component and propagate the props through that...