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
4.6k views
in Technique[技术] by (71.8m points)

reactjs - Why do I get Undefined

I have this code:

import React, {Component} from 'react';
import {connect} from 'react-redux';
import {compose} from "redux";
import {firestoreConnect} from "react-redux-firebase";
import {getAllowance} from "../utils";


class MemberList extends Component {
    render() {
        const {allowance} = this.props
        console.log('allowance', allowance);
        return (
            <div className="members">
                {getAllowance(allowance, 'Jackie')}
            </div>
        );
    }
}

const mapStateToProps = (state) => {
    return {
        allowance: state.firestore.ordered.allowance,
        users: state.firestore.ordered.users,
        auth: state.firebase.auth,
    }
}

export default compose(
    connect(mapStateToProps),
    firestoreConnect([
        {collection: 'users'},
        {collection: 'allowance'}
    ])
)(MemberList);

Without {getAllowance(allowance, 'Jackie')} I get the data in the console: [console.log][1] [1]: https://i.stack.imgur.com/ZOhHu.png

But when I want to work with the data -> allowance = undefined

code getAllowance

export const getAllowance = (allowances, firstName) => {
    console.log('getAllowance props', allowances);
    console.log('getAllowance id', firstName);
    //console.log('id', id);
    const kid = allowances.filter(allowance => allowance.selectedKid === firstName);
    console.log('kid', kid);
    if(allowances && Array.isArray(kid) && kid.length){
        return kid[0].totalAmount;
    }
    else {
        return 0;
    }
}

This works in another component but here it fails and I don't know why...


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

1 Reply

0 votes
by (71.8m points)
等待大神解答

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

...