【发布时间】:2018-05-11 07:12:32
【问题描述】:
我正在使用 Reactjs 制作一个简单的专辑列表,单击每个项目后,我必须获取该专辑的所有照片,所以我想改变状态 (album_Id) .. 问题是我无法更改状态,我不知道如何独立加载第二页(显示一个相册的所有照片)。 我需要你的帮助。
这是我的代码 (App.js):
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import Photolist from './photolist';
// import logo from './logo.svg';
import './App.css';
class App extends Component {
constructor(props){
super(props);
this.state = {
currentAlbumId: null,
CurrentPhotoId: null
};
this.updateCurrentAlbum = this.updateCurrentAlbum.bind(this);
}
updateCurrentAlbum(id) {
this.setState({currentAlbumId : id})
}
render() {
const tab_albums = [
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "http://placehold.it/600/92c952",
"thumbnailUrl": "http://placehold.it/150/92c952"
},
{
"albumId": 1,
"id": 2,
"title": "reprehenderit est deserunt velit ipsam",
"url": "http://placehold.it/600/771796",
"thumbnailUrl": "http://placehold.it/150/771796"
},
{
"albumId": 2,
"id": 66,
"title": "provident rerum voluptatem illo asperiores qui maiores",
"url": "http://placehold.it/600/ee0a7e",
"thumbnailUrl": "http://placehold.it/150/ee0a7e"
},
{
"albumId": 2,
"id": 67,
"title": "veritatis labore ipsum unde aut quam dolores",
"url": "http://placehold.it/600/1279e9",
"thumbnailUrl": "http://placehold.it/150/1279e9"
}
];
const albumsIds = [];
tab_albums.map((album_model) => {
return (
albumsIds.indexOf(album_model.albumId) === -1 ? albumsIds.push(album_model.albumId) : null
)
});
var album_style = {"background": "#cccccc", "marginBottom": "10px", "borderLeft": "5px solid red"};
var style_div = {"width": "50%", "float": "left"};
const liste_album = albumsIds.map((alb_id) => {
return (
<Router key={alb_id}>
<li style={album_style} >
<Link to={"/photolist/"+alb_id} onClick={() => this.updateCurrentAlbum(alb_id)}>
Album : { alb_id }
</Link>
<Route path="/photolist" component={Photolist}/>
</li>
</Router>
)
});
return (
<div className="App">
<div style={style_div}>
<ul>{liste_album} </ul>
</div>
<div style={style_div}>
<button>wishlist</button>
</div>
</div>
);
}
}
export default App;
【问题讨论】:
-
看起来你有代码来设置状态。您能否更具体地说明“我无法更改状态”的含义?预期与实际的行为是什么?
-
@Jacob 我的意思是 currentAlbumId,我想我必须在每次点击后更新它以加载每个相册的照片
-
只需制作一张可点击的照片,然后将带有道具的 currentAlbumId 传递到另一个页面。然后用 包裹按钮
标签: javascript reactjs jsx