F
F
frolldoll2018-04-12 20:37:21
API
frolldoll, 2018-04-12 20:37:21

How would you do this in React Redux (rendering data received from the API)?

You need to render the data on the page received from this request

https://api.coinmarketcap.com/v1/ticker/bitcoin/?c...

Like what action to do what to write in the reducer , how would you implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Suntsev, 2018-04-12
@GreyCrew

Something like this?
(I assembled it on my knee, you don’t think that they will write the program for you)

import {appName} from '../config'
import {Record} from 'immutable'
import {createSelector} from 'reselect'

/**
 * Constants
 * */
export const moduleName = 'bitcoin'
const prefix = `${appName}/${moduleName}`
export const INIT = `${prefix}/INIT`

/**
 * Reducer
 * */

const ReducerRecord  = Record({
    bitcoin: null
})

export default function reducer(state = new ReducerRecord(), action) {
    const {type, payload} = action

    switch (type) {
        case INIT:
            return state.set('bitcoin', payload)
        default:
            return state
    }
}
/**
 * Selectors
 * */

export const bitcoinSelector = state => state[moduleName].bitcoin

/**
 * Action Creators
 * */

export function bitcointInit(url) {
    return (dispatch) => {
      fetch(url)
        .then((data) => {
         dispatch({
            type: INIT,
            payload: data
        })
      });
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question