2017-11-03 5 views
0

탑재 할 때 redux 저장소에 항목 데이터를 채워야하는 컨테이너 구성 요소가 있는데 구성 요소는 올바르게 id 인 작업을 호출합니다 요청했다. 이 작업은 수행되지 않는 dispatch 함수를 반환하므로 데이터는 검색되지 않습니다.Redux-thunk 동작은 실행되지 않는 함수를 반환하므로 데이터를 가져 오지 않습니다.

내가 뭘 잘못하고 있니?

내 컨테이너 구성 요소는 이것이다 :

내 활동 파일은 다음과 같습니다
import { connect } from "react-redux"; 
import React, { Component } from "react"; 

import fetchItem from "../actions/itemActions"; 
import ItemDetails from "../components/ItemDetails"; 

class ItemDetailsContainer extends Component { 
    state = { 
    item: null 
    }; 

    componentDidMount() { 
    return fetchItem(this.props.match.params.number); 
    } 

    render() { 
    return <ItemDetails {...this.props} />; 
    } 
} 

const mapStateToProps = (state, props) => { 
    return { 
    item: state.item, 
    user_location: state.user_location 
    }; 
}; 

export default connect(mapStateToProps)(ItemDetailsContainer); 

: fetchItem를 호출 할 때

import * as actions from "../constants/constants"; 
import fetch from "isomorphic-fetch"; 

const itemDetailsRequest = id => { 
    return { 
    type: actions.FETCH_ITEM_REQUEST, 
    id, 
    receivedAt: Date.now() 
    }; 
}; 

const itemDetailsSuccess = (id, json) => { 
    return { 
    type: actions.FETCH_ITEM_SUCCESS, 
    items: json.map(child => child), 
    receivedAt: Date.now() 
    }; 
}; 

const itemDetailsFailure = (id, json) => { 
    return { 
    type: actions.FETCH_ITEM_FAILURE, 
    error: json.error, 
    receivedAt: Date.now() 
    }; 
}; 

const fetchItem = id => { 
    console.log(`returning dispatch function for item, id: ${id}`); 
    return function(dispatch) { 
    console.log("fetching Item: " + id); 
    dispatch(itemDetailsRequest(id)); 
    fetch(`item.json?id=${id}`) 
     .then(
     response => response.json(), 
     error => console.log("AN ERROR OCCURED.", error) 
    ) 
     .then(json => { 
     dispatch(itemDetailsSuccess(id, json)); 
     }); 
    }; 
}; 

export default fetchItem; 
+0

는'fetchItem'는 함수가'시도 반환 fetchItem (this.props.match.params.number)() ; '그것을 호출합니다. –

답변

1

당신은 dispatch를 사용하는 것을 잊었다 :

componentDidMount() { 
    return this.props.dispatch(fetchItem(this.props.match.params.number)); 
} 

또는 경우

dispatch을 직접 사용하고 싶지는 않습니다.

const mapStateToProps = (state, props) => { 
    return { 
    item: state.item, 
    user_location: state.user_location 
    }; 
}; 


const mapDispatchToProps = dispatch => { 
    return({ 
    fetchItem: (number) => dispatch(fetchItem(number)), 
    }) 
} 

export default connect(mapStateToProps, mapDispatchToProps)(ItemDetailsContainer); 

을 그리고 componentDidMount에서 : 다음 componentDidMount에 직접 mapDispatchToProps을 사용할 수 있습니다

componentDidMount() { 
    return this.props.fetchItem(this.props.match.params.number); 
} 
+0

감사합니다.'fetchItem' 함수를 디스패치하면 함수가 실제로 실행되지만 redux가'type' 속성을 갖기를 기대합니다. 이것은 오류입니다. '잡히지 않은 오류 : 액션에 정의되지 않은 "유형이 없을 수 있습니다" 재산. 상수를 잘못 입력 했습니까? '나는 당신의 제안을 모두 시도했다. –

+0

** 상수 ** 파일을 보여 주시겠습니까? 이 오류를 직면 할 때 그것은 다음 중 하나입니다 : 1. 당신이 파견 할 때 ** 타입 **의 ** 속성을 가지고 있지 않습니다, ** ** 타입 **의 값은 정의되지 않았습니다 (= 당신이 부르는 상수는 귀하의 상수 파일) – yuantonito

+0

이전에 테스트하기 위해 이전에 이러한 상수에 대한 특정 상수를 별도의 상수 파일로 옮겼으나 가져 오기를 새 이름으로 변경하지 못했습니다. 감사합니다. 이제이 비트가 작동합니다. –