homespun json API (json-server --watch db.json)를 사용하여 플럭스 저장소의 상태를 채우려고합니다. 부모 컴포넌트의 componentWillMount
에서 Store 함수를 호출하여 Axios로 데이터를로드합니다.액시즈를 통해 상태를 채우는 데 문제가 발생했습니다.
내가 참조를 위해 다음 튜토리얼에서 찾고있다 : http://mediatemple.net/blog/tips/loading-and-using-external-data-in-react/ https://www.toptal.com/react/tips-and-practices 등을. 여기
내가 당기는 오전 JSON 데이터이다import { EventEmitter } from 'events'
import dispatcher from '../dispatcher'
import axios from 'axios'
import uuid from 'uuid4'
class TodoStore extends EventEmitter {
constructor() {
super()
this.state = {
todos: [],
showCompletedTodos: true,
showIncompletedTodos: true
}
this.deleteTodo = this.deleteTodo.bind(this)
this.toggleTodo = this.toggleTodo.bind(this)
this.toggleShowCompletedTodos = this.toggleShowCompletedTodos.bind(this)
this.toggleShowIncompletedTodos = this.toggleShowIncompletedTodos.bind(this)
}
...
fetchUserState() {
let th = this
console.log(th)
this.serverRequest =
axios.get('http://localhost:3000/todos')
.then(function(result) {
console.log(result.data)
th.state.todos = result.data
})
}
}
const todoStore = new TodoStore
dispatcher.register(todoStore.handleActions.bind(todoStore))
export default todoStore
요점 :
[
{
"complete": "false",
"edit": "false",
"id": "uuid()",
"text": "Go Shopping"
},
{
"complete": "false",
"edit": "false",
"id": "uuid()",
"text": "Pay Water Bill"
}
]
db.json 요지 여기
https://gist.github.com/WebRuin/548f6073ca533016503d34c36116b8de 내가 부르고 플럭스 저장소 발췌 인 전체 파일 중 https://gist.github.com/WebRuin/efbbf4296c566f6eb58cc6d641bee4ba
콘솔 로깅을 통해 축 데이터 요청이 원하는 데이터로 완료되고 이 채워지는 것으로 표시되는 this
의 로그가 표시되지만 Chrome의 반응 플러그인에는 데이터가 표시되지 않으므로 앱에 데이터가 표시되지 않습니다.
내가 뭘 잘못하고 있니?
"th.state.todos = result.data"대신 "th.setState ({todos : result.data})"시도하십시오. –
아주 좋은 질문입니다. – prosti