简体   繁体   English

我不确定为什么我在使用React Redux时看到未定义

[英]I'm unsure as to why i'm seeing undefined while using react redux

So i'm trying to integrate redux into my react code and i'm getting a problem where the array i'm trying to access is undefined. 所以我试图将redux集成到我的react代码中,并且遇到一个问题,即我尝试访问的数组未定义。 It is the console.log statement in the first block of code. 这是第一段代码中的console.log语句。 I'm just trying to read data from the redux store. 我只是想从redux存储中读取数据。 I've been trying to figure out why this is a problem as my object types should be fine and it should be connected correctly. 我一直试图弄清为什么这是一个问题,因为我的对象类型应该很好并且应该正确连接。 I'm just not sure at this point. 我现在不确定。

InterestList.js InterestList.js

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

class InterestList extends Component{
    constructor(props){
        super(props)
    }
    render() {
        return(
            <div className = 'container'>
                {console.log(this.props.skillList)}

            </div>
        )
    }
}

const mapStateToProps = (state) =>{
    return{skillList: state.skillList}
}

export default connect(mapStateToProps)(InterestList)

Reducer for parent component 母组件的减速器

import {ADD_INTEREST} from "./Constant";
const initialState = {
    skillList: [{skillName: '', skillValue: '', interestValue: ''}]
}
export function interestCard(state = initialState, action){
    switch(action.type){
        case ADD_INTEREST:
            return [...state, action.payload]
        default:
            return state
    }
}

Action for parent component 父组件的操作

import {ADD_INTEREST} from './Constant'

export const addInterest = interest => ({
    type: ADD_INTEREST,
    payload: interest
})

Combine Reducers 组合减速器

import {combineReducers} from 'redux'
import {interestCard} from "./InterestCardReducer";

const rootReducer = combineReducers({
    interestCard,
})

export default rootReducer

Looking at this : 看这个:

const rootReducer = combineReducers({
    interestCard,
})

you should access the state like this : state.interestCard.skillList and not this way state.skillList . 您应该像这样访问状态: state.interestCard.skillList而不是state.skillList

Something else that's wrong is that inside the reducer interestCard , your initial state suggests that the state is an object, not an array ! 另一个错误是在reducer interestCard ,您的初始状态表明该状态是对象,而不是数组! so this return [...state, action.payload] will throw an error. 因此,此return [...state, action.payload]将引发错误。

And Last thing : 最后一件事:

   <div className = 'container'>
        {console.log(this.props.skillList)}
    </div>

This won't work, because console.log is not evaluated to a String or to a React element or to a list of these 2. 这是行不通的,因为console.log没有被评估为字符串,React元素或这两个列表。

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

相关问题 为什么我在 Angular 应用程序的浏览器控制台中看到 Cannot read properties of undefined (reading 'indexOf') - why does I'm seeing Cannot read properties of undefined (reading 'indexOf') in my browser console for Angular application 如果我两次使用相同的react / redux组件,它们会共享状态吗? - If I'm using the same react/redux component twice, will they share state? 我不确定使用xmlHttpRequest接收responseText - Receiving a responseText using xmlHttpRequest that i'm unsure about 为什么即使我使用的是 Set,我的表上的两列也会出现重复值? - Why am I seeing repeated values for both columns on my table even though I'm using a Set? State 更改为未定义值。 我正在使用带有 redux-persist 的 redux-toolkit - State changing to undefined value. I'm using redux-toolkit with redux-persist 我不确定是否要滥用SwitchNavigator或只是缺少一些东西 - I'm unsure if I'm misusing the SwitchNavigator or just missing something 为什么我使用布尔对象变得不确定? - Why i'm getting undefined using Boolean object? 为什么在 React.js 中使用 useEffect 钩子时会收到这些警告? - Why I'm getting these warnings while using useEffect hook in React.js? Javascript数组未定义......我不确定为什么 - Javascript array is undefined… and I'm not sure why 为什么我得到的var undefined - Why I'm getting the var undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM