反应原生无法呈现JSON键“车型”

时间:2017-11-14 06:57:00

标签: json react-native eslint

如果这个问题有点令人困惑,我很抱歉。

基本上,我有这个JSON响应,

{
"timestamp": 1510636692,
"verb": "GET",
"object": "car",
"data": [
    {
        "car-type": "muscle",
        "startDate": 1509950129,
        "ident": "864495033993046",
        "endDate": 1509950301
    }
]
}

我试图在渲染函数中调用car-type的值。长话短说,我使用react-redux和其他库来请求JSON响应,保存并将其显示在屏幕上

下面是我的渲染功能

import React, { Component } from 'react';
import { View, Text, ScrollView } from 'react-native';
import { Card, Button } from 'react-native-elements';
import { connect } from 'react-redux';

class SafetyScreen extends Component {

    onButtonPressClose = () => {
        this.props.navigation.navigate('home');
}

render() {
    return (
        <ScrollView>
            <Card title="Driver's Info">
                <View>
                    <View style={{ flexDirection: 'row' }}>
                        <Text style={{ fontSize: 15, fontWeight: 'bold' }}>Car Type : </Text>
                        <Text>{this.props.informations.data[0].car-type}</Text>
                    </View>
                </View>
            </Card>
            <Card>
                <Button
                    small
                    title="Back"
                    backgroundColor="#94b8b8"
                    onPress={this.onButtonPressClose}
                />
            </Card>
         </ScrollView>
        );
    }
}

function mapStateToProps({ informations }) {
return { informations };
}

export default connect(mapStateToProps)(SafetyScreen);

我的工作区中也有.eslintrc,它显示car-type的错误

错误1:[eslint]中缀运算符必须间隔(space-infix-ops)

错误2:[eslint]'type'未定义(no-undef)

我想也许我可以通过在space-infix-ops中添加忽略no-undef.eslintrc的规则来忽略这些错误,但不是。它后来在我的模拟器上显示错误ReferenceError : Can't find variable: type

其中一个解决方案可能是将JSON响应中的密钥car-type更改为carType,但我无权更改API中的任何内容,因此这不是一个选项。

请帮帮我。

谢谢。

0 个答案:

没有答案