Reactjs redux状态中的数组未定义
在这里,我面临一个关于react-redux的小问题。。。。。 我正在获取未定义的状态属性(数组)。。。。原因可能是什么? 这是我的Reducers.js代码Reactjs redux状态中的数组未定义,reactjs,redux,Reactjs,Redux,在这里,我面临一个关于react-redux的小问题。。。。。 我正在获取未定义的状态属性(数组)。。。。原因可能是什么? 这是我的Reducers.js代码 const initialState = { cList: [], //I'm getting this array undefined... selectedCatagory: "", } export const reducerForCatagoryList = (state = i
const initialState = {
cList: [], //I'm getting this array undefined...
selectedCatagory: "",
}
export const reducerForCatagoryList = (state = initialState , action) => {
switch(action.type){
case "CATAGORY_LIST":
let lis = [...state.cList,action.payload];
return Object.assign({},state,{ cList: lis });
default:
return state;
}
}
Actions.js代码是:
export const catagoryList = (list) => {
return {
type: "CATAGORY_LIST",
payload: list
}
}
//This array is just for testing purpose....
const list = [{
cName: 1,
heading: "Heading",
link: ""
},
{
cName: 2,
heading: "Heading2",
link: ""
}]
const mapStateToProps = (state) => {
console.log("state is: ",state);
return {
cList: state.cList
}
}
const mapDispatchToProps = (dispatch) => {
return {
catagoryList: (list) => {
dispatch(catagoryList(list));
}
}
}
class MainPage extends Component{
render(){
console.log("Props are: ",this.props)
return(
<div id = "Cards-div">
{this.props.catagoryList(list)}
{
createCards(this.props.cList) //I want to send Array inside redux store (cList) to this......
}
</div>
)
}
}
export default connect(mapStateToProps,mapDispatchToProps)(MainPage);
我的组件代码是:
export const catagoryList = (list) => {
return {
type: "CATAGORY_LIST",
payload: list
}
}
//This array is just for testing purpose....
const list = [{
cName: 1,
heading: "Heading",
link: ""
},
{
cName: 2,
heading: "Heading2",
link: ""
}]
const mapStateToProps = (state) => {
console.log("state is: ",state);
return {
cList: state.cList
}
}
const mapDispatchToProps = (dispatch) => {
return {
catagoryList: (list) => {
dispatch(catagoryList(list));
}
}
}
class MainPage extends Component{
render(){
console.log("Props are: ",this.props)
return(
<div id = "Cards-div">
{this.props.catagoryList(list)}
{
createCards(this.props.cList) //I want to send Array inside redux store (cList) to this......
}
</div>
)
}
}
export default connect(mapStateToProps,mapDispatchToProps)(MainPage);
//此数组仅用于测试目的。。。。
常数列表=[{
cName:1,
标题:“标题”,
链接:“
},
{
cName:2,
标题:“标题2”,
链接:“
}]
常量mapStateToProps=(状态)=>{
log(“状态为:”,状态);
返回{
cList:state.cList
}
}
const mapDispatchToProps=(调度)=>{
返回{
分类列表:(列表)=>{
调度(分类列表(列表));
}
}
}
类主页扩展组件{
render(){
log(“道具是:”,this.Props)
返回(
{this.props.catagoryList(列表)}
{
createCards(this.props.cList)//我想将redux存储(cList)中的数组发送到此。。。。。。
}
)
}
}
导出默认连接(mapStateToProps、mapDispatchToProps)(主页);
那么,有人能解释一下这里出了什么问题吗?代码似乎不错。出于开发目的,请尝试添加
redux logger
或类似的中间件,以便更轻松地调试。此外,我不确定您为什么要在return中调用函数。将它添加到componentDidMount(){}
好的,谢谢,我会试试的!