最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React.js passing one components variables to another component and vice-versa - Stack Overflow

programmeradmin1浏览0评论

I've just started to learn react. If I have a component with two variables, what is the easiest way to access these in another component? Use props, useState, useContext? How would you do to use the two variables inside ComponentTwo.

import React from 'react';

ComponentOne = () => {

varOne = Mike;
varTwo = Tyson;

Return(
<div>
</div>
)}

export default ComponentOne

import React from 'react';
import ComponentOne from '../OtherFolder/';

ComponentTwo = () => {

Return(
<div>
<p>{varOne}, {varTwo}</p>
</div>
)}

export default ComponentTwo```


I've just started to learn react. If I have a component with two variables, what is the easiest way to access these in another component? Use props, useState, useContext? How would you do to use the two variables inside ComponentTwo.

import React from 'react';

ComponentOne = () => {

varOne = Mike;
varTwo = Tyson;

Return(
<div>
</div>
)}

export default ComponentOne

import React from 'react';
import ComponentOne from '../OtherFolder/';

ComponentTwo = () => {

Return(
<div>
<p>{varOne}, {varTwo}</p>
</div>
)}

export default ComponentTwo```


Share Improve this question edited Dec 14, 2021 at 5:55 Apoorva Chikara 8,7733 gold badges23 silver badges38 bronze badges asked Dec 14, 2021 at 5:39 Svante PålssonSvante Pålsson 1371 gold badge1 silver badge12 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

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.

  1. 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.

  1. 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.

  1. 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.

Above examples miss one important need, which is to pass data using a method argument.

In parent component,

    const [argdata, setargData] = React.useState(""); // to reflect change in parent when child data change.
      const parentCallback = (args: string) => {
        setargData(args);
        console.log("onVariablesChanged called", vardata);
      };
    
      <ChildComponent
       childFunc={parentCallback} 
      />
    
    In Child Component,
    
     export type ChildComponentProps = {childFunc:(args:string) => void}
        
        export default function ChildComponent(childFunc):ChildComponentProps) {
         try{childFunc('anything');}
        catch(ex) {}
        } 

export var and then importing is another solution. Context object can also be used.

发布评论

评论列表(0)

  1. 暂无评论