Instruction:

We get a response:

[
    {
        "id": 122431,
        "title": null,
        "categories": null,
        "content": null
    },
    {
        "id": 122275,
        "title": "Sup!",
        "categories": "Cat5",
        "content": "Buy ETH"
    },
    {
        "id": 122234,
        "title": "Hey bob",
        "categories": "my Category",
        "content": "This is my content"
    }
]

To get this object we need a special action:

import axios from 'axios'

export const FETCH_POSTS = 'fetch_posts';

const ROOT_URL = 'http://reduxblog.herokuapp.com/api';
const API_KEY = '?key=ROOKA112';

export function fetchPosts() {
    const request = axios.get(`${ROOT_URL}/posts${API_KEY}`);
    return {
        type: FETCH_POSTS,
        payload: request
    };
}

And in reducer we should use lodash. In a way _.mapKeys(data, 'id')

import {FETCH_POSTS} from "../actions/index"
import _ from 'lodash'

export default function (state = {}, action) {
    switch (action.type) {
        case FETCH_POSTS:
            return _.mapKeys(action.payload.data, 'id');
        default:
            return state;
    }
}

results matching ""

    No results matching ""