reduxでreactを使用した際にdispatchがreducerに反応しないのですがなぜでしょうか。
ご教示いただければ幸いです。
/actions/news-get-actions.js
js
1export const GET_NEWS = 'GET_NEWS' 2 3const getNewsRequest = () => { 4 return{ 5 type: GET_NEWS 6 } 7} 8 9export const GET_NEWS_SUCCESS = 'GET_NEWS_SUCCESS' 10const getNewsSuccess = (json) => { 11 return{ 12 type: GET_NEWS_SCCESS, 13 news: json 14 } 15} 16 17export const GET_NEWS_FAILURE = 'GET_NEWS_FAILURE' 18const getNewsFailure = (error) => { 19 type: GET_NEWS_FAILURE, 20 error 21} 22 23export const getNews = () => { 24 return(dispatch) => { 25 return request.get('/assets/json/news.json') 26 .then(res => { 27 dispatch(getNewsSuccess(res.data)) 28 } 29 ).catch(err => 30 dispatch(getNewsFailure(err)) 31 ) 32 } 33}
上記のコードにaxiosはimportして{request}にしております。
reducer
js
1import { 2 GET_NEWS_REQUEST, GET_NEWS_SUCCESS, GET_NEWS_FAILURE 3} from '../../actions/news-get-actions' 4 5export default function newsGetReducer (state = {}, action) { 6 console.log(action) 7 switch (action.type) { 8 case GET_NEWS_REQUEST: 9 return { ...data } 10 case GET_NEWS_SUCCESS: 11 return { ...data } 12 case GET_NEWS_FAILURE: 13 return { ...data } 14 default: 15 return state 16 } 17}
こんにちは。 次の2点のソースコードもご質問に追加で載せて頂くことは可能でしょうか?① なんらかのトリガーによってGET_NEWS アクションを dispatch しているコンポーネント ② Redux とconnectされる いわゆるcontainer コンポーネント 以上の2点です。それと質問ですが、newsGetReducer の中に data という変数が出てきていますが、これはどこで定義されていますか?または action のプロパティか何かでしょうか?
あなたの回答
tips
プレビュー