0
hackernews api를 사용하여 내 응답 객체를 사용하여 개별 스토리 제목을 브라우저에 렌더링하는 속성 "데이터"를 얻으려고합니다. 원래 데이터는 개별 이야기를 나타내는 ID의 배열에 있습니다. 나는 이드에서 모든 이야기를 얻기 위해 두 번째 요청을한다. 현재 모든 제목은 브라우저에서 한 줄로 빠르게 나타납니다.지도를 사용하여 반응 요소를 생성하는 방법
class App extends Component {
constructor() {
super()
this.state = {
storyIds: null,
title: null
}
} // constructor end
componentDidMount() {
axios.get('https://hacker- news.firebaseio.com/v0/beststories.json?print=pretty')
// use arrow to access state
.then((res) => {
// res.data is array of story ids
this.setState({storyIds: res.data})
})
.then((res) => {
console.log(this.state.storyIds)
this.state.storyIds.map ((id) => {
axios.get(`https://hackernews.firebaseio.com/v0/item/${id}/title.json?print=pretty`)
.then((res) =>{
this.setState({title: res.data})
console.log(res.data)
})
})
})
} // didMount end
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to Hackernews Top Stories</h2>
</div>
<p>{this.state.title}</p>
</div>
);
}
}
export default App;
지도를 사용하여 response.data를 루프 처리하고 개별 줄에 제목을 생성하려면 어떻게합니까?
감사합니다. Promise 객체에 대해 읽었습니다. 너무 익숙하지 않습니다. Promise.all 섹션에서 확인할 값은 ".then (responses => ..."? –
) 비동기 JS에 필수적입니다. 'axios.get()'을 호출하면 Promise.resolve()는 명시 적으로 반환하고 전달한 것만 제외하면 액시스 호출과 마찬가지로 해결 된 약속입니다. 실제 데이터 페이로드 – Rorschach120
Promise.all은 전달 된 배열의 모든 요소가 해석 될 때 해결되며 결과 배열로 해결 된 약속을 반환합니다.이 경우 모든 응답의 배열입니다. 해결 된 데이터 ..... 그것은 '해결'이라는 단어를 많이 사용합니다. LOL 의미가 있기를 바랍니다. – Rorschach120