[英]React - passing jSON data to child of child components
I am building an image grid which has three components: the parent Playlist.js component, inside which is the PlayListAlbumsList.js component, inside which resides the PlaylistAlbumInfoOverlay.js component. 我正在构建一个包含三个组件的图像网格:父级Playlist.js组件,其中是PlayListAlbumsList.js组件,内部是PlaylistAlbumInfoOverlay.js组件。
I've got the PlaylistAlbumsList.js successfully loading images from my JSON file with their classNames and keys, but now I need to grab {list.albumTitle} and get that into the h1 of the PlaylistAlbumInfoOverlay.js component. 我已经获得了PlaylistAlbumsList.js及其类名和键从JSON文件成功加载图像的功能,但是现在我需要抓取{list.albumTitle}并将其放入PlaylistAlbumInfoOverlay.js组件的h1中。
I'm not sure how to configure it. 我不确定如何配置它。 Here are the three components:
这是三个组成部分:
THE PARENT PLAYLIST COMPONENT: 家长播放列表组件:
import React from 'react';
import PlaylistAlbumsList from './PlaylistAlbumsList';
var data;
$.getJSON("./json/playlist.json", function(json){
data = json;
});
var Playlist = React.createClass({
getInitialState: function() {
return {
currentPlaylist: []
};
},
componentDidMount: function() {
var playlistImages = [];
// Fake an AJAX request with a setTimeout function
setTimeout(function(){
playlistImages=data.images;
// Set state when the request completes
this.setState({
currentPlaylist: playlistImages
});
}.bind(this), 10);
},
render() {
return (
<div id="playlist_page">
<div className="grid-wrap">
<PlaylistAlbumsList list={this.state.currentPlaylist} />
</div>
</div>
)
}
});
module.exports = Playlist;
THE CHILD UL COMPONENT: 儿童UL组件:
import React from 'react';
import PlaylistAlbumInfoOverlay from './PlaylistAlbumInfoOverlay';
var PlaylistAlbumsList = React.createClass({
render: function() {
return (
<ul className="container-fluid">
{this.props.list.map(function(list){
return (
<li className={list.className} key={list.id}>
<PlaylistAlbumInfoOverlay />
<img src={list.src} width='100%' />
</li>
)
}.bind(this))}
</ul>
)
}
});
module.exports = PlaylistAlbumsList;
THE CHILD OF CHILD OVERLAY COMPONENT: 儿童重叠部分的儿童:
import React from 'react';
const PlaylistAlbumInfoOverlay = () => (
<div className="playlist-album-info-overlay">
<h2>{list.albumTitle}</h2> // doesn't work
</div>
);
PlaylistAlbumInfoOverlay.propTypes = {
albumTitle: React.PropTypes.string // doesn't work
};
export default PlaylistAlbumInfoOverlay;
You're really close, just pass list
in as a prop in the PlaylistAlbumsList
file like so: 您真的很亲密,只需将
list
作为道具传递给PlaylistAlbumsList
文件,如下所示:
import React from 'react';
import PlaylistAlbumInfoOverlay from './PlaylistAlbumInfoOverlay';
var PlaylistAlbumsList = React.createClass({
render: function() {
return (
<ul className="container-fluid">
{this.props.list.map(function(list){
return (
<li className={list.className} key={list.id}>
<PlaylistAlbumInfoOverlay list={list} /> // changed
<img src={list.src} width='100%' />
</li>
)
}.bind(this))}
</ul>
)
}
});
module.exports = PlaylistAlbumsList;
And then access that as a prop in PlaylistAlbumInfoOverlay
: 然后在
PlaylistAlbumInfoOverlay
中作为道具访问它:
import React from 'react';
const PlaylistAlbumInfoOverlay = () => (
<div className="playlist-album-info-overlay">
<h2>{this.props.list.albumTitle}</h2> // changed
</div>
);
PlaylistAlbumInfoOverlay.propTypes = {
albumTitle: React.PropTypes.string // doesn't work
};
export default PlaylistAlbumInfoOverlay;
When calling the child of the child component: 调用子组件的子组件时:
<PlaylistAlbumInfoOverlay title={list.albumTitle} />
And your PlaylistAlbumInfoOverlay: 和您的PlaylistAlbumInfoOverlay:
const PlaylistAlbumInfoOverlay = ({ title }) => (
<div className="playlist-album-info-overlay">
<h2>{title}</h2>
</div>
);
PlaylistAlbumInfoOverlay.propTypes = {
title: React.PropTypes.string
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.