2016-08-09 7 views
2

원격 서버에서 데이터를 가져와야하는 redux 앱에 구성 요소가 있습니다.Redux에서 데이터 가져 오기

어디서 전화를 걸고 모든 논리를 처리 할 수 ​​있습니까? 액션 크리에이터? componentDidMount와 같은 라이프 사이클 메소드의 스마트 컴포넌트?

답변

1

componentDidMount은 올바른 API 호출 위치입니다. 데이터를 얼마나 빨리 원하는지에 따라 때로는 componentWillMount에 원할 수도 있습니다.

데이터를 가져 오는 작업을 만드는 데 도움이되는 redux-thunk를 살펴 보는 것도 좋습니다.

https://github.com/gaearon/redux-thunk

1

그것은 상황에 따라 달라집니다.

// SomeComponent.js 

// ... other methods ... 

constructor(props) { 
    super(props); 

    this.state = { 
    todos: [], 
    }; 
} 

componentDidMount() { 
    // `axios` is ajax library 
    axios.get('/todos') 
    .then(todos => { 
     this.setState({ 
     todos: todos 
     }); 
    }); 
} 
: -

당신이 API 호출을 수행하기 위해, 내 의견으로는, smart/dumb 구성 요소 개념, 가장 좋은 장소를 사용하여 응용 프로그램을 작성하는 경우 componentDidMount 라이프 사이클 콜백입니다