제목에서 알 수 있듯이 JSON에서 가져온 데이터가 들어있는 React 구성 요소를 fetch()를 사용하여로드하려고합니다.React - JSON로드 및 구성 요소 구성
api 호출은 정상적으로 작동하지만 데이터를 렌더링 할 수 없습니다.
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
user: {}
}
}
getUserById(uId) {
fetch(`https://jsonplaceholder.typicode.com/users/${uId}`)
.then((response) => {
return response.json()
})
.then((json) => {
return json;
});
}
componentDidMount() {
this.setState({
user: this.getUserById(4)
})
}
render() {
return (
<div>
<h1>User</h1>
<p>ID: {this.state.user.id}</p>
</div>
);
}
}
ReactDOM.render(
<App/>,
document.getElementById("container")
);
<div id="container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
이 문제 해결에 어떤 생각 : 여기
코드인가?
헤헤, 그래. 나는 그걸 스 니펫에 넣었다. 메모가 내 문제를 해결했습니다. 감사합니다! – user3660122