search

Home  >  Q&A  >  body text

Application of methods to obtain data from other components in React

<p>I have a constant that receives a json data through fetch. The code is as follows: </p> <p><code>const [user, setUser] = useState([]);</code></p> <p><code> Asynchronous function users() { const result = wait fetch (</code>My API `,{ })</p> <pre class="brush:php;toolbar:false;">const data = await result.json(); setUser(data);`</pre> <p>I want to display her data in another component, like only name or only ID. </p> <p>I want to put the name in this little blue block: this is another component</p> <p>More about the second component</p>
P粉449281068P粉449281068455 days ago387

reply all(1)I'll reply

  • P粉633075725

    P粉6330757252023-09-04 17:05:58

    The first component is the component that gets the data, and the second component is the component that displays the data, because you receive user data through props.

    function FirstComponent(){
    const [user, setUser] = useState([]);
    
     return (
       <SecondComponent user={user} />
     )
    }
    
    function SecondComponent({user}){
     console.log(user)
    
     return (
       //这里是你展示图片的代码
     )
    }

    reply
    0
  • Cancelreply