简体   繁体   English

redux形式的mapStateToProps TypeError:即使我可以console.log状态对象并查看值,也无法读取未定义的属性“值”

[英]redux-form mapStateToProps TypeError: Cannot read property 'values' of undefined even though i can console.log the state object and see the values

So I'm using redux-form and I'm able to console.log(state) and see the values out from the state object in review.js and see that they are preset in state.form.PayForm.values.Dancer , however if I try to reference that in the review page itself as a console.log directly I get the error described above. 所以我正在使用redux-form,并且能够console.log(state)并在review.js从状态对象中看到值,并看到它们已在state.form.PayForm.values.Dancer中预设,但是,如果我尝试直接在评论页面中将其作为console.log引用,则会收到上述错误。 I have a very similar setup working perfectly on another project and haven't ever really had a problem with redux-form before. 我有一个非常类似的设置,可以完美地在另一个项目上运行,并且以前从未真正遇到过redux-form的问题。

Any help or insight in any direction would help. 任何帮助或任何方向的见识都会有所帮助。 Thanks ahead of time for taking the time to look at this. 提前感谢您抽出宝贵的时间对此进行研究。 Let me know if there is any other information I can supply. 让我知道我是否可以提供其他信息。

Also for the sake of being easier on some people's eyes I took some screenshots of the code as well. 另外,为了使某些人更轻松,我还对代码进行了截图。

payform.js payform.js

import React, { Component } from 'react';
import { Field, reduxForm, reset } from 'redux-form'
import PayFormField from './PayFormField';


class PayForm extends Component{


  render(){
    return(
      <div>
        <label style={{color:'black'}}> Please select lead or follow 
        </label>
        <form onSubmit={this.props.handleSubmit(this.props.onFormSubmit)}>
          <Field type="text" component={PayFormField} name="Dancer" />
          <button style={{color:'black'}} type="submit">Review 
           Purchase
          </button>
       </form>
     </div>
    )
  }
}


export default reduxForm({
form:'PayForm',
destroyOnUnmount:false
})(PayForm)

payformfield.js payformfield.js

  import React, { Component } from 'react'
  import { Field } from 'redux-form';




  export default ({input,meta})=>{    
  return( 
    <div>
      <Field component="select" type="select" {...input} >
        <option 
        style={{color:'black'}}
        />
          <option value="Lead">Lead</option>
          <option value="Follow">Follow</option>
      </Field>  
    </div>
  )    
 }

payformholder.js payformholder.js

import React, {Component } from 'react';
import PayForm from './payForm'
import Review from './review'


class payFormHolder extends Component{
  state = { showreview: 'false'} 

  renderContent(){
    if(this.state.showreview=='true'){
      return(<Review />)
    }
    else{
      return(
        <PayForm 
        onFormSubmit={()=> this.setState({ showreview:'true' })}
      />
      )
    }
  }
  render(){
    return(
      <div>
        {this.renderContent()}
      </div>
    )
  }
}
export default payFormHolder;

review.js review.js

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

const Review = ({formValues}) => {
    return(
      <div> im the review 
        {formValues.values.Dancer} 
      </div>
    )
  }
function mapStateToProps(state){
  return{
    formValues: state.form.PayForm
  };
}
export default connect(mapStateToProps)(Review) 

just in case here in the packacge.json 以防万一在packacge.json中

  {
    "name": "salsanewyear",
    "version": "0.0.0",
    "private": true,
    "scripts": {
      "start": "node ./bin/www & node apiServer.js"
    },
    "devDependencies": {
      "nodemon": "^1.17.5",
      "webpack": "^3.11.0"
    },
    "dependencies": {
      "axios": "^0.18.0",
      "babel-core": "^6.26.3",
      "babel-loader": "^7.1.4",
      "babel-preset-env": "^1.7.0",
      "babel-preset-es2015": "^6.24.1",
      "babel-preset-react": "^6.24.1",
      "babel-preset-stage-1": "^6.24.1",
      "cookie-parser": "~1.4.3",
      "debug": "~2.6.9",
      "express": "~4.16.0",
      "http-errors": "~1.6.2",
      "http-proxy": "^1.17.0",
      "jade": "~1.11.0",
      "lodash": "^4.17.10",
      "morgan": "~1.9.0",
      "paypal-rest-sdk": "^1.8.1",
      "react": "^16.4.1",
      "react-bootstrap": "^0.32.1",
      "react-dom": "^16.4.1",
      "react-redux": "^5.0.7",
      "react-router": "^4.3.1",
      "react-router-dom": "^4.3.1",
      "redux": "^4.0.0",
      "redux-form": "^6.8.0",
      "redux-thunk": "^2.3.0"
    }
  }

client.js client.js

  "use strict"
  import React from 'react';
  import {render} from 'react-dom';
  import {Provider} from 'react-redux';
  import { createStore, applyMiddleware } from 'redux';
  import ReduxThunk from 'redux-thunk'
  import reducers from './reducers/index';

  import LoadinPage from './components/LoadinPage';
  import Footer from './components/footer';




  const store = createStore(reducers, applyMiddleware(ReduxThunk));


  render(
    <Provider store={store}>
    <LoadinPage />
    </Provider>, document.getElementById("app"));

index.js(reducer) index.js(减速)

  import {combineReducers} from 'redux';
  import { reducer as form } from 'redux-form';
  export default combineReducers({  form });

payform.js payform.js

payformfield.js payformfield.js

payformholder.js review.js payformholder.js review.js

At present, the form state is destroyed in the Redux store when the form is unmounted. 目前,在卸载表单时,Redux存储中的表单状态已被破坏。

This is because there is a typo in the configuration object passed to the reduxForm HOC. 这是因为在传递给reduxForm HOC的配置对象中有一个错字。

You want to set destroyOnUnmount to be false and not distroyOnUnmount in the configuration object passed to reduxForm HOC. 你想设置destroyOnUnmountfalse ,而不是 distroyOnUnmount 传递给reduxForm HOC的配置对象。

暂无
暂无

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

相关问题 即使console.log清楚地显示了具有所需属性的对象,也无法访问对象属性(React Redux) - Can't access object property even though console.log clearly shows an Object with the needed property (React Redux) 通过 mapStateToProps 访问 redux-form 值 - Access redux-form values through mapStateToProps 属性未定义,但console.log可以看到它 - property undefined but console.log can see it 未捕获(承诺)TypeError:无法设置 null 的属性“innerHTML” - 我可以在 console.log() 中看到值 - Uncaught (in promise) TypeError: Cannot set property 'innerHTML' of null - I can see value in console.log() TypeError:无法读取未定义的属性“状态”,即使已经绑定 - TypeError: Cannot read property 'state' of undefined, even though already binded console.log()“无法读取未定义的值”错误 - console.log () "cannot read values of undefined" error 即使对象上的console.log显示属性值,对象的属性也未定义 - Property of object is undefined, even though console.log on object is showing the property value React state 未定义,即使 console.log 显示它 - React state is undefined , even though console.log shows it Backbone.js model.get()返回&#39;undefined&#39;,即使我可以在console.log中看到属性 - Backbone.js model.get() returning 'undefined' even though I can see the attributes in console.log JS 说 object 是未定义的,即使它显示在 console.log 中 - JS says an object is undefined even though it shows with console.log
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM