Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

reactjs - Push an array elements in a redux state returns `undefined`

I wanna use redux to do @pilchard's solution in this thread. It was not working in redux. where is going wrong with my codes?

action/index.js:

export const setCities = (city) => {
    console.log(city)
    return {
        type: GET_CITIES_BY_SPECIAL_STATE,
        payload: city
    }
}

reducers/mainReducer.js

import {
    GET_CITIES_BY_SPECIAL_STATE
} from '../actions/types';

const initialState = {
    cities: []
}
const mainReducers = (state = initialState, action) => {
    switch (action.type) {
        case GET_CITIES_BY_SPECIAL_STATE:
            return {...state, cities: state.cities.concat(action.payload)}
        default:
            return state;
    }
}
export default mainReducers; 

reducers/index.js:

import { combineReducers } from 'redux';
import mainReducers from "../reducers/main";
const reducers = combineReducers({
    setCitiesReducer: mainReducers
});
export default reducers;

And this is how i use them:

import React, {useEffect} from "react";
import getCities from "../functions/getCities";
import RadioList from "./RadioList";
import {connect} from 'react-redux'
import {setCities} from '../redux/actions'

function ListOfStates(props) {
    let labels = [],
        cities = [];
    useEffect(() => {
       getCities(props.cityState).then((array) => {
           for (let i = 0; i < array.response.length; i++)
               cities.push(array.response[i]);
           props.setCities([...cities]);
       });
    }, []);
    return (<>
        {
            props.city.map((labels, index) =>
                <RadioList active={(labels === props.active)} key={index} label={labels}/>)
        }
    </>);
}

const mapStatesToProps = state => {
    return {
        city: state.setCitiesReducer.city,
    }
}
export default connect(mapStatesToProps, {setCities})(ListOfStates);

The error is:

TypeError: Cannot read property 'map' of undefined

question from:https://stackoverflow.com/questions/65870050/push-an-array-elements-in-a-redux-state-returns-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

function ListOfStates(props) {
    let labels = [],
        cities = [];
    useEffect(() => {
       getCities(props.cityState).then((array) => {
           for (let i = 0; i < array.response.length; i++)
               cities.push(array.response[i]);
           props.setCities([...cities]);
       });
    }, []);
    return (<>
        {
            props.city.map((labels, index) =>
                <RadioList active={(labels === props.active)} key={index} label={labels}/>)
        }
    </>);
}

const mapStatesToProps = state => {
    return {
        city: state.setCitiesReducer.cities,
    }
}
export default connect(mapStatesToProps, {setCities})(ListOfStates);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...