React Native: Possible Unhandled Promise Rejection (id: 0)

so I'm trying to learn more about Redux via React-Native.

I am trying to issue an HTTPS request using Axios in order to fetch data from a web server. everything is working fine, i log the action to the console and the payload is the data i need. but it still throws "Can not read property" data of null "TypeError".

following code:

import React, { Component } from 'react';
import ReduxThunk from 'redux-thunk';
import { StackNavigator } from 'react-navigation';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import reducers from '../reducers';
import ReduxTestObj from '../components/ReduxTestObj';

export default class ReduxTest extends Component {
    render() {
        return (
          <Provider store={createStore(reducers, {}, applyMiddleware(ReduxThunk))}>
            <ReduxTestObj />
          </Provider>
        );
      }
    }

      

Here is ReduxTestObj

import React, { Component } from 'react';
import _ from 'lodash';
import { View } from 'react-native';
import { connect } from 'react-redux';
import DevBanner from './DevBanner';
import Header from './Header';
import { actionCreator } from '../actions';

class ReduxTestObj extends Component {
  componentWillMount() {
    this.props.actionCreator('urlIWantToGoTo');
  }
  getBannerText() {
    if (this.props.loading) {
      return 'PULLING DATA. GIVE ME A SECOND';
    }
    const rObj = _.map(this.state.data[1].recipient_list);
    const rName = [];
    for (let i = 0; i < rObj.length; i++) {
      rName[i] = rObj[i].team_key.substring(3);
    }
    const winnerMSG = `Teams ${rName[0]}, ${rName[1]}, ${rName[2]}, ${rName[3]} Won`;
    return winnerMSG;
  }
  render() {
    return (
      <View>
        <Header
          text={'Redux Testing'}
        />
        <DevBanner
         message={this.getBannerText()}
        />
      </View>
    );
  }
}

const mapStateToProps = state => {
  return {
    data: state.tba.data,
    loading: state.tba.loading
  };
};

export default connect(mapStateToProps, { actionCreator })(ReduxTestObj);

      

Here is Action creator

import axios from 'axios';
import { TBA_PULL } from './Types';


export const actionCreator = (url) => {
  return (dispatch) => {
    axios({
      method: 'get',
      url,
      responseType: 'json',
      headers: {
        'Auth-Key': 
'Hidden For Obvious Reasons'
      },
      baseURL: 'theBaseUrlOfTheWebsite'
    }).then(response => {
      dispatch({ type: TBA_PULL, payload: response.data });
    });
  };
};

      

And here is the reducer

import { TBA_PULL } from '../actions/Types';

const INITIAL_STATE = { data: [], loading: true };
export default (state = INITIAL_STATE, action) => {
  console.log(action);
  switch (action.type) {
    case TBA_PULL:
      return { ...state, loading: false, data: action.payload };
    default:
      return state;
  }
};

      

As stated above the console.log command (action) is called and the data is correct. but it keeps throwing the following error: Error1

I tried to change things up, google search, search, cut Action Reducer to make it as simple as possible by just returning a string. and he refuses to work. Does anyone run into a problem like this or know how to fix it?

Thank you for your time.

EDIT: I also logged in 'this' as the first line of getBannerText () in ReduxTestObj and successfully returned this.props.data as the data I want and this.props.loading as false. However, it still throws the same error.

+3


source to share


1 answer


Welp. I guess I was just wrong.

i was calling this.state.data instead of this.props.data in getBannerText ().



which solved the problem.

0


source







All Articles