简体   繁体   English

如何在 React.js 中将 state 作为道具从父母传递给孩子,然后从该孩子传递给它的孩子

[英]How to pass state from parent to child as props then from that child to it s child as props in React.js

For now everything is static data.现在一切都是 static 数据。

I have a parent sate: an array containing 4 objects like so:我有一个父状态:一个包含 4 个对象的数组,如下所示:

 function App(){

  const [searchResults,setSearchResults] = 
    useState([
      {
      name: 'name1',
      artist: 'artist1',
      album: 'album1',
      id: 1
    },
    {
      name: 'name2',
      artist: 'artist2',
      album: 'album2',
      id: 2
    },
    {
      name: 'name3',
      artist: 'artist3',
      album: 'album3',
      id: 3
    },
    {
      name: 'name4',
      artist: 'artist4',
      album: 'album4',
      id: 4
    }
  ]);

i pass this state to the child in the return with this it works as expected:我将这个 state 传递给孩子,并按预期工作:

return (
 <SearchResults searchResults = {searchResults} />
)

and from i pass it to:并从我传递给:

import React,{useEffect,useState} from "react";
import './SearchResults.css';
import TrackList from '../TrackList/TrackList';


export default function SearchResults({searchResults}) {
    return (
        <div className="SearchResults">
            <h2>Results</h2>
             <TrackList tracks = {searchResults} />
        </div>
    )
}

now is where things got me confused.现在是事情让我感到困惑的地方。

i try to map the data from the array to the child of this component like this(this is the whole component):我尝试 map 将数组中的数据传输到该组件的子组件,如下所示(这是整个组件):

import React,{useEffect,useState} from "react";
import './TrackList.css';
import Track from '../Track/Track'

export default function TrackList(props) {
    return(
        <div className="TrackList">
            {
                props.tracks.map( track => {
                    <Track track = {track} key = {track.id} />
                })
            }
            
        </div>
    )
}

and i get this error with the local server displaying a white screen [1]: https://i.stack.imgur.com/5niRQ.png我在本地服务器显示白屏时收到此错误[1]: https://i.stack.imgur.com/5niRQ.png

  • i tried destructuring and assigning props value to a variable before mapping but didn't work.我尝试在映射之前解构并将道具值分配给变量,但没有奏效。
  • also when i remove the.map i can see that has a props with expected data in it using react dev tools.同样,当我删除.map 时,我可以使用反应开发工具看到其中有一个带有预期数据的道具。

You always can view what you transfer to props with console.您始终可以使用控制台查看传输到道具的内容。

console.log(props);

You need add console.log(props);您需要添加console.log(props); before return statement, like this在 return 语句之前,像这样

import React,{useEffect,useState} from "react";
import './TrackList.css';
import Track from '../Track/Track'

export default function TrackList(props) {

console.log(props);
    return(
        <div className="TrackList">
            
        </div>
    )
}

you need pass props to SearchResults component not a param您需要将道具传递给 SearchResults 组件而不是参数

like this像这样

import React,{useEffect,useState} from "react";
import './SearchResults.css';
import TrackList from '../TrackList/TrackList';


export default function SearchResults(props) {
  return (
    <div className="SearchResults">
        <h2>Results</h2>
         <TrackList tracks = {props.searchResults} />
    </div>
  )
}

I have reproduced your code in a sandbox, but I have excluded the Tracks component.我已经在沙箱中复制了您的代码,但我已经排除了Tracks组件。 I don't see any bug, please refer to it and let me know if you need any more help.我没有看到任何错误,请参考它,如果您需要更多帮助,请告诉我。 Also, when using map or any other function you need to either use the return keyword or avoid using the curly braces {} .此外,当使用map或任何其他 function 时,您需要使用return关键字或避免使用花括号{}
For example, inside TrackList function:例如,在TrackList function 内部:

{
     props.tracks.map( track => { //here avoid this or use 'return' keyword inside
          <Track track = {track} key = {track.id} />
     })
}

//Like this:
{
     props.tracks.map( track => 
          <Track track = {track} key = {track.id} />
     )
}

//or
{
     props.tracks.map( track => 
          { return <Track track = {track} key = {track.id} /> }
     )
}

Link to Sandbox 沙盒链接
Please accept the answer or upvote if this helps, thank you!如果有帮助,请采纳答案或点赞,谢谢!

The issue is not in the components that you have mentioned, it is in the Playlist component, wherein you have loaded the TrackList component without the tracks prop.问题不在您提到的组件中,而是在Playlist组件中,您在其中加载了TrackList组件而没有tracks道具。 Please accept the answer if it helps your case.如果对您的情况有帮助,请接受答案。 Thank you!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM