简体   繁体   English

redux所需的原型未定义

[英]redux required proptypes undefined

I've been bashing my head in trying to play around with this. 我一直在努力尝试解决这个问题。 FOr somereason my props aren't connecting in redux. 出于某种原因,我的道具未在Redux中连接。 Here is my error 这是我的错误

VM803:36 Warning: Failed prop type: The prop `apiData` is marked as required 
in `TestApiPage`, but its value is `undefined`.
in TestApiPage (created by RouterContext)
in RouterContext (created by Router)
in Router (created by Root)
in Provider (created by Root)
in Root
in AppContainer
VM803:36 Warning: Failed prop type: The prop `actions` is marked as required 
in `TestApiPage`, but its value is `undefined`.
in TestApiPage (created by RouterContext)
in RouterContext (created by Router)
in Router (created by Root)
in Provider (created by Root)
in Root
in AppContainer

Here is my code TestApiPage 这是我的代码TestApiPage

import React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as actions from '../actions/testApiActions';
import {TestApiForm} from '../components/TestApi';

export const TestApiPage = (props) => {
    console.log(props);
  return (
    <TestApiForm
    apiData={props.apiData}

/>
);
};

TestApiPage.propTypes = {
  actions: PropTypes.object.isRequired,
  apiData: PropTypes.object.isRequired
};

function mapStateToProps(state) {
  return {
    apiData: state.apiData 
  };
}

function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(actions, dispatch)
  };
}
    export default connect(
  mapStateToProps,
  mapDispatchToProps
)(TestApiPage);

testApiActions testApiActions

export function callTestApiAction() {
return function (dispatch) {
return dispatch({
  type: types.CALL_TEST_API,
  message: "blah",

});
 };
}

testApiReducer testApiReducer

import {CALL_TEST_API} from '../constants/actionTypes';
import objectAssign from 'object-assign';
import initialState from './initialState';

export function testApiReducer(state = initialState.apiData, action) {

  switch (action.type) {
    case CALL_TEST_API:

      return objectAssign({}, state, {message: action.message});
    default:
      return state;
  }
}

initialState initialState

export default {

   apiData: {
        message: "You haven't called your api yet! :("
    }
};

When I use Redux dev tools i do see my state there and logs in the connect show it is working. 当我使用Redux开发工具时,我确实在那里看到了我的状态,并在connect中登录以表明它正在工作。 not sure what's going on. 不知道发生了什么。 Any clues? 有什么线索吗?

So your problem is that your initial state is the contents of apiData , and not an object that contains apiData . 因此,您的问题是您的初始状态是apiData的内容,而不是包含apiData的对象。

In other words you set state object equal to apiData : 换句话说,将状态对象设置为等于apiData

export function testApiReducer(state = initialState.apiData, action) {
//                                     ^ here you set state = apiData
}

But you want to extract apiData as a property of state: 但是您想将apiData提取为state的属性:

function mapStateToProps(state) {
  return {
    apiData: state.apiData 
    //       ^ but here you expect state.apiData
  };
}

Change the reducer to use the entire initial state instead: 将reducer更改为使用整个初始状态:

// here's your reducer if your state is `{ apiData }`
export function testApiReducer(state = initialState, action) {
//                                     ^ use only initialState here
  switch (action.type) {
    case CALL_TEST_API:

      // make sure you properly update the internal `apiData` part of the state
      return objectAssign({}, state, {
        apiData: Object.assign({}, state.apiData, {
          message: action.message
        });
    default:
      return state;
  }
}

Or change anywhere you have state.apiData to use just state . 或更改您拥有state.apiData任何地方以仅使用state

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM