【问题标题】:unable to pass props to components in react-redux无法将道具传递给 react-redux 中的组件
【发布时间】:2017-05-15 04:32:22
【问题描述】:

我正在尝试使用 react-redux 将道具传递给多个组件。我接收并尝试传递的数据来自数据库,因此涉及异步性。因为 props 在碰到组件时没有定义,所以我的应用程序抛出了一个未定义的错误。在组件中抛出错误后,它会继续解析状态,但是一旦定义了状态,它就不会使用新的 props 重新渲染组件。另一个掘金......我正在使用 combineReducers。在使用 combineReducers(仅使用婴儿减速器)之前,我没有这个问题。它是在我制作了另一个减速器并将其与 (baby-reducer) 组合之后开始的。知道这里发生了什么吗?为什么我的组件不会重新渲染?

这是我正在渲染组件的容器:

    import React,{Component} from 'react'
    import store from '../store'
    import {connect} from 'react-redux';
    import BabyProfile from '../components/BabyProfile'
    import Weight from '../components/Weight'
    import Height from '../components/Height'
    import Feed from '../components/Feed'
    import Sleep from '../components/Sleep'
    import Diaper from '../components/Diaper'


    class BabyProfileContainer extends Component {

      render(){
        // console.log("RENDER PROPS", this.props)
        const  {baby, weight, height, feed, sleep, diapers} = this.props

        return(
          <div>
            <BabyProfile baby={baby}/>
            <Weight weight={weight}/>
            <Height height={height}/>
            <Feed  feed={feed}/>
            <Sleep sleep={sleep}/>
            <Diaper diapers={diapers}/>
          </div>
        )
      }
    }

    const mapStateToProps = (state, ownProps) => {
      // console.log("MSTP Baby Container", state, "OWN PROPS", ownProps)
      return {
        baby: state.baby,
        diapers: state.diapers,
        weight: state.weight,
        height: state.height,
        sleep: state.sleep,
        feed: state.feeding
      }
    }



    export default connect(mapStateToProps)(BabyProfileContainer)

这是我的婴儿减速器:

    import {RECEIVE_BABY} from '../constants'


    const initialBabyState = {
      baby: {},
      diapers: [],
      weight: [],
      height: [],
      feeding: [],
      sleep: []

    }

    export default function(state = initialBabyState, action){
    console.log('BABY REducer')

    const newState = Object.assign({}, state)

       switch (action.type){

         case RECEIVE_BABY:
         newState.baby = action.baby;
         newState.diapers = action.diapers
         newState.weight = action.weight;
         newState.height = action.height;
         newState.feeding = action.feeding;
         newState.sleep = action.sleep

         break;




         default:
          return state

       }
       return newState

    }

这是我的combinedReducer:

    import {combineReducers} from 'redux'
    import baby from './baby-reducer'
    import parent from './parent-reducer'



    export default combineReducers({
      baby,
      parent

    })

这是我的体重分量:

    import React from 'react';
    import {Line} from 'react-chartjs-2'


    export default function(weight){


    console.log("IN WEIGHT", weight)
    var weightArr = weight.weight

    const weightData = {
      labels: weightArr.map(instance=>instance.date.slice(0,10)),
      datasets:[{
        label: "baby weight",
        backgroundColor: 'rgba(75,192,192,1)',
        data: weightArr.map(instance =>(instance.weight))
      }]
    }

    const options ={

        maintainAspectRatio: false,
        xAxes: [{
          stacked: true,
                  ticks: {
                      beginAtZero:true
                  }
              }],
        yAxes: [{ticks: {
            beginAtZero:true
        }
        }]
      }



    return (
      <div className="baby">
        <div>
          <h3>I'm In weight component</h3>
        </div>
        <div className="weight"></div>
        {
        weightArr.map((weight,index) => (
          <div key={index}>
            <span>{ weight.weight}</span>
          </div>
        ))

      }
      <div className="Chart">
        <Line data={weightData} width={200} height={200} options={options}/>
      </div>
      </div>
    );
    }

这是我的动作创建者:

    import {RECEIVE_BABY} from '../constants'
    import axios from 'axios'



    export const receiveBaby = (baby,weight,height,sleep,diaper,feeding) =>({
      type: RECEIVE_BABY,
      baby: baby,
      weight: weight,
      feeding: feeding,
      height: height,
      sleep: sleep,
      diapers: diaper

    })



    export const getBabyById = babyId => {
      console.log("GET BABY BY ID")
      return dispatch => {
        Promise
          .all([
            axios.get(`/api/baby/${babyId}`),
            axios.get(`/api/baby/${babyId}/weight`),
            axios.get(`/api/baby/${babyId}/height`),
            axios.get(`/api/baby/${babyId}/sleep`),
            axios.get(`/api/baby/${babyId}/diaper`),
            axios.get(`/api/baby/${babyId}/feed`)
          ])
          .then(results => results.map(r => r.data))
          .then(results => {
            console.log("THIS IS RESULTS in Action assadkjasdfkjl;", results)
            dispatch(receiveBaby(...results));
          })
          .catch(function(err){
            console.log(err)
          })
      };
    };

Here is a snapshot from chrome devTools of the error and the subsequent state

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 您已经在您的状态中将weight 定义为一个空数组。但在您的屏幕截图中,我看到weight 的值为undefined。如果我在你的位置,我会首先检查我的操作是否正确更新weight。我觉得您作为参数传递给weight 操作的值不正确。如果操作正常工作,您能否分享您发送操作的位置的container 以及您连接到商店的根容器?
  • 用你的行动更新你的问题。您还想利用lifecycle methods in React
  • 嗨,我已经添加了我的动作创建器和我的重量组件。 Harkirat Saluja,我认为重量没问题,因为它在商店中是正确的,问题是我的组件在商店更新状态后不会重新渲染,因此道具没有分配给重量组件(这对我的所有组件都是如此不仅仅是重量)。

标签: javascript components react-redux reducers


【解决方案1】:

我认为这与以下事实有关:当您组合减速器时,您需要通过以下方式将它们包含在 mapStateToProps 中:

const mapStateToProps = (state, ownProps) => {
      // console.log("MSTP Baby Container", state, "OWN PROPS", ownProps)
      return {
        baby: state.parent.baby,
        diapers: state.parent.diapers,
        ...
      }
    }

基本上,你必须确保这里连接了合适的reducer。

【讨论】:

    猜你喜欢
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多