使用react-redux连接graphql查询响应,获取ReferenceError:找不到变量

时间:2017-08-26 18:10:47

标签: react-native react-redux graphql apollo-client

我正在使用apollo和react-redux处理一个使用graphql的react-native应用程序。

我有以下graphql查询,它为登录用户返回以下信息(使用JTW令牌):

enter image description here

我正在尝试运行上述查询,并通过redux将响应发送到react-native应用程序。

这是我的 graphql / queries / me.js

import { gql } from 'react-apollo';

export default gql`
    {
        me {
            avatar
            username
            firstName
            lastName
        }
    }
`;

我正试图在我的HomeScreen上使用它,用户在成功登录后被分派到这里:

// ...
import { graphql, compose, withApollo } from 'react-apollo';
import { connect } from 'react-redux';

import ME_QUERY from '../graphql/queries/me';


class HomeScreen extends Component {
    componentDidMount() {
        this._getUserInfo();
    }

    _getUserInfo = async () => {
        const { data: { me } } = await this.props.client.query({ query: ME_QUERY });
        this.props.getUserInfo(me);
    };

    render () {
        const { data } = this.props;
        if (data.loading) {
            return (
                <Root>
                    <ActivityIndicator size="large" />
                </Root>
            );
        }
        return (
            // snipped
        );
    };
}

export default withApollo(compose(
    connect(undefined, { getUserInfo }),
    graphql(GET_TWEETS_QUERY)
)(HomeScreen));

这是我的 actions / user.js (redux操作)

// ...

export function getUserInfo() {
    return {
        type: 'GET_USER_INFO',
        info
    }
}

user的缩减器:

export default (state = initialState, action) => {
    switch (action.type) {
        // ...
        case 'GET_USER_INFO':
            return {
                ...state,
                info: action.info
            };
        default:
            return state;
    }
};

运行getUserInfo查询后我们收到了相关信息,我试图在名为HeaderAvatar的组件上使用它,如下所示:

import React, { Component } from 'react';
import styled from 'styled-components/native';
import Touchable from '@appandflow/touchable';
import { connect } from 'react-redux';

const AVATAR_SIZE = 30;
const AVATAR_RADIUS = AVATAR_SIZE / 2;

const Avatar = styled.Image`
    height: ${AVATAR_SIZE};
    width: ${AVATAR_SIZE};
    borderRadius: ${AVATAR_RADIUS};
`;

const Button = styled(Touchable).attrs({
    feedback: 'opacity',
    hitSlop: { top: 20, bottom: 20, right: 20, left: 20 }
})`
    marginLeft: 15;
    justifyContent: center;
    alignItems: center;
`;

class HeaderAvatar extends Component {
    state = { }
    render () {
        if (!this.props.info) {
            // snipped
        }
        return (
            <Button>
                <Avatar source={{ uri: this.props.info.avatar }} />
            </Button>
        );
    }
}

export default connect(state => ({ info: state.user.info }))(HeaderAvatar);

知道为什么我无法从状态对象中获取info字段吗?

我知道查询正在执行,我正在加载信息,我通过在me函数中执行_getUserInfo()对象的console.log来验证它,我可以看到数据。

P.S。对于长篇文章感到抱歉,我不知道如何描述这个问题。

这是Android模拟器中出现错误的方式:http://i.imgur.com/VdT2TJq.png

1 个答案:

答案 0 :(得分:1)

您发现该错误是因为您在动作创建者中使用了未声明的变量:

export function getUserInfo() {
    return {
        type: 'GET_USER_INFO',
        info
    }
}

您只需要将info添加到getUserInfo

所采用的参数中