简体   繁体   中英

React + Redux , error updating state using axios

I am using React JS with Redux, I am trying to fetch data from API but encountered two error.

ACTION CREATOR: The below code supposed to get API data and return it. I don't know how to return fetched data

import axios from 'axios';
const fetchlist = (id)=>{    
    const post = axios.get(`https://jsonplaceholder.typicode.com/posts/${id}`).then(response=>response.data)

    return{

        type:'FETCH',
        payload:post.data
    }
}

export default fetchlist

REDUCER FUNCTION the below code is to update the post state but gets error Block is redundant no-lone-blocks

const post=(state={post:{}},action)=>{
switch(action.type){
    case 'FETCH':{    // ERROR OCCURES IN THIS LINE
        return { post:action.payload}
    };
    default: return state
}}

export default post

In switch statement remove the curly brackets before return it should be:

case 'FETCH':
        return { post:action.payload}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM