0

Expected Results

  • After this.props.startGetPrices() is called in my main Container.
  • The API is hit and returns data to the reducer.
  • Which then should update the Redux state and thus update state in the Container.

Results

  • Action is called, API returns formatted data in Array, assets sent into reducer, but Redux state is not updated.

enter image description here

enter image description here

enter image description here

My store.ts file:

import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunkMiddleware from 'redux-thunk'

import { getLatest } from './services/api'

export interface IinitialState {
  assets: any[];
  wallets: any[];
  defaultCurrency: string;
}

export interface IPricesRes {
  data: IPriceData
}

export interface IPriceData {
  base: string;
  date: string;
  rates: any;
  success: boolean;
  timestamp: number;
}

const defaultInitialState = {
  assets: [],
  wallets: [],
  defaultCurrency: ''
}

// ACTION TYPES
export const actionTypes = {
  GET_PRICES: 'GET_PRICES'
}

// const updateAssets = (state: IinitialState, action: any) => {
//   const { assets } = state;
//   const newArray = new Array(action.payload, ...assets)[0];
//   return newArray;
// }

// REDUCER
export const reducer = (state = defaultInitialState, action: any) => {
  switch (action.type) {
    case actionTypes.GET_PRICES: {
      const { payload } = action;
      console.log('payload', payload);
      // const newAssets = updateAssets(state, action);
      // console.log('newAssets', newAssets);
      return {
        // assets: new Array(payload, ...state.assets)[0],
        assets: payload,
        ...state
      };
    }

    default:
      return state;
  }
}

// ACTIONS CREATORS
export const actionGetPrices = (rates: any) => ({
  type: actionTypes.GET_PRICES,
  payload: rates
});

// ACTIONS
export const startGetPrices = () => (dispatch: any) => getLatest().then((ratesArray) => {
  dispatch(actionGetPrices(ratesArray));
});

// @ts-ignore
export function initializeStore(initialState = defaultInitialState) {
  return createStore(
    reducer,
    initialState,
    composeWithDevTools(applyMiddleware(thunkMiddleware))
  )
}

My Container file FiatWallet.tsx

import React from 'react'
import { connect } from 'react-redux'

import { startGetPrices, IPricesRes } from '../store'
import { CurrencySelector, Header, Prices, Navigation } from '../components'

interface IProps {
  assets: [];
  wallets: [];
  defaultCurreny: string;
  startGetPrices(): IPricesRes;
}

class FiatWallet extends React.PureComponent<IProps> {
  componentDidMount() {
    console.log('FiatWallet componentDidMount...');
    this.props.startGetPrices();
  }

  public render() {
    const { assets } = this.props;
    console.log('assets from redux state:', assets);
    return (
      <section>
        <CurrencySelector />
        <Header />
        <Prices prices={assets} />
        <Navigation />
      </section>
    );
  }     
}

const mapDispatchToProps = (dispatch: any) => ({
  startGetPrices: () => dispatch(startGetPrices())
});

const mapStateToProps = (state: any) => ({
  assets: state.assets,
  wallets: state.wallets,
  defaultCurrency: state.defaultCurrency
});

export const BoardJest = FiatWallet;

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

My Converter Util

// Takes rates { key : value } pairs and converts into Array of objects.
export const ratesIntoArray = ({ data: { rates } }: any) =>
  Object.keys(rates).map(data => new Object({ [data]: rates[data]}));

1 Answer 1

1

Change this line

return {
    // assets: new Array(payload, ...state.assets)[0],
    assets: payload,
    ...state
  };

To this

return {
    ...state
    // assets: new Array(payload, ...state.assets)[0],
    assets: payload,    
  };

The problem is that you're replacing the new values with the old ones by doing the destructuring after the assigment.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I knew it was something simple :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.