【发布时间】:2019-02-26 10:17:18
【问题描述】:
我无法更新需要从 songList 获取值的状态歌曲。如何将歌曲更新到 songList ?它与组件生命周期有关吗?运行以下代码时,会抛出“songsList is undefined”错误。 const songList 在 render 中。
import React, { Component } from 'react';
import logo from './components/Logo/box8.png';
import './App.css';
import SearchBox from './components/SearchBox/SearchBox';
import SongCards from './components/SongCards/SongCards';
import 'tachyons';
import axios from 'axios';
class App extends Component {
state = {
songs : [],
searchField: '',
entries: []
};
componentDidMount() {
axios.get(`https://itunes.apple.com/in/rss/topalbums/limit=100/json`)
.then(response =>
{this.setState({ entries: response.data.feed.entry });
});
}
onSearchChange=(event)=>{
this.setState({songs : songsList})
this.setState({searchField : event.target.value})
const filteredSongs = this.state.songs.filter(song =>{
return song.title.toLowerCase().includes(this.state.searchField.toLowerCase())
});
}
render(){
const songsList = this.state.entries.map(entries => {
return (
<SongCards
key={entries.id.label}
artist={entries["im:artist"].label}
image={entries["im:image"][2].label}
link={entries.id.label}
price={entries["im:price"].label}
date={entries["im:releaseDate"].label}
title={entries.title.label}
/>
);
});
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
</header>
<SearchBox searchChange= {this.onSearchChange}/>
{songsList}
</div>
);
}
}
export default App;
【问题讨论】:
-
您的具体问题是什么?歌曲列表未定义?您的 this.state.entries 看起来如何?你能做控制台日志并更新有问题的数据,以便我们检查吗
-
它是一个包含歌曲详细信息的对象对象类别:{ id: {label: "itunes.apple.com/in/album/aravindha-sameth...inal-motion-picture-soundtrack-ep/1436841209?uo=2", im:artist: {标签:“Thaman S.”,属性:{...}} im:contentType:{im:contentType:{...},im:image:(3)im:itemCount:{label:“4”} im:name: {label:“Aravindha Sametha(电影原声带)-EP”} im:price:{label:“₹60”,im:releaseDate:{label:“2018-09-20T00:00:00-07:00” , 版权:{label:“℗ 2018 Zee Music Company”} 标题:{label:“Aravindha Sametha (Original Motion Picture Soundtrack)”}
-
你能把这些数据放在你的问题中吗?你也没有确认你的确切问题
-
由于格式问题,我无法将此对象数据放入问题中。我想将 this.state.songs 更新为我为 const songList 获得的输出。
-
我想在搜索字段中搜索到的歌曲