简体   繁体   中英

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

so i'm trying to learn more about Redux through React-Native.

i'm trying to issue a HTTPS request with Axios to pull data from a web-server. everything runs fine, i console log the action and the payload is the data i need. but it still throws a 'Cannot read property 'data' of null' TypeError.

the following is my 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 the 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 the 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(action) prints out and the data it has is correct. yet it continues to throw the following error: Error1

I've tried changing things around, googling, searching, gutting out the Action Reducer to make it as basic as possible by just returning a string. and it refuses to work. Has anyone run into an issue similar to this or know how to fix it?

Thank you for your time.

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

Welp. I guess i was just making a mistake.

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

that solved the issue.

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