简体   繁体   中英

why undefined [object Object],[object Object] on react-redux?

I have an arry:

classesAll:[0:{id:1,title:'test1'},1:{id:2,title:'test2'}]

It dosent return the excepted value of my array when I 'console.log(this.props.classes.classesAll)' it to console. Instead it shows the following: undefined [object Object],[object Object]


async componentDidMount() {
    await this.props.showAllClasses();
    console.log("***", this.props.classes.classesAll)
  }
Redux action:

export const showAllClasses = () => {
    return async dispatch => {
       dispatch(pageLoading(true));
            await  Api.get(`classes.php`).then(res => {
                //this.state.all = res.data;
                const classesAll = res.data.data;
                state.classesAll = classesAll;
            });
       await dispatch(onChangeClasessAll(state.classesAll));
       dispatch(pageLoading(false));
        };

    };

    export const onChangeClasessAll = classesAll => {
        const type =  CLASSES_ALL;
         return   { type , classesAll};
      };

Image

Cannot predict the exact reason, just wanted know what does following line does, state.classesAll = classesAll;

Because your request is async. you should console it when the request is returned. in react-native 0.6, you can do it in the getDerivedStateFromProps(props, state) method

static getDerivedStateFromProps(props, state) {
    console.log(props.classes.classesAll)
}

before the react-native 0.6, you should do it in the componentWillReceiveProps() method

componentWillReceiveProps(nextProps) {
 console.log(props.classes.classesAll)
}

Please improve your action and make sure you are getting the res.data from server.

export const showAllClasses = () => dispatch => {
   dispatch(pageLoading(true));
   const res = await Api.get(`classes.php`);
   dispatch(onChangeClasessAll(res.data));
   dispatch(pageLoading(false));
};

Then console the this.props in render method just like

render(){
  console.log(this.props);
  return
    ......
}

You will see all the props in the console so you can easily get the data from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM