1

I am sorry if the question a little bit confusing.

Basically, I have this JSON response,

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

and I am trying to call the value for car-type in render function. Long story short, I use react-redux and other library to request the JSON response, save it and show it to the screen

Below is my render function

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);

I also have .eslintrc in my workspace, and it shows me error for car-type

Error 1 : [eslint] Infix operators must be spaced (space-infix-ops)

Error 2 : [eslint] 'type' is not defined (no-undef)

I thought maybe I can just ignore these errors by adding rules to ignore space-infix-ops and no-undef in .eslintrc but no. It later shows me an error on my simulator saying ReferenceError : Can't find variable: type

One of the solution might be changing the key car-type in the JSON response to carType but I do not have any access to change anything in the API so this is not an option.

Please help me.

Thank you.

Ling
  • 891
  • 5
  • 17
  • 40
  • 1
    `this.props.informations.data[0]["car-type"]` – bennygenel Nov 14 '17 at 06:58
  • 2
    Possible duplicate of [How do I reference a javascript object property with a hyphen in it?](https://stackoverflow.com/questions/7122609/how-do-i-reference-a-javascript-object-property-with-a-hyphen-in-it) – Ryan Turnbull Nov 14 '17 at 07:09
  • thank you for your help bennygenel . it works. didn't thought of that earlier. @RyanTurnbull thanks for sharing the question here. It would be a great reference to me in the future – Ling Nov 14 '17 at 07:52

0 Answers0