[英]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.