mlbrulz
mlbrulz

Reputation: 1

How to create an object in react native?

I just started learning react native and I am having issues with creating an object. Here is the code

import React, { Component } from 'react';
import {
  View,
  Text,
  StyleSheet,
} from 'react-native';

export default class object {
    name: 'joe'
    age: 27
    country: 'France'
}

And when I try to instantiate it in another class like this

 import React, { Component } from 'react';
    import {
      View,
      Text,
      StyleSheet,
    } from 'react-native';

    import object from './object.js'

    export default class MyComponent extends Component {

      var man = new Object();

      render() {
        return (
          <View style={styles.container}>
            <Text>{man.age}</Text>
          </View>
        );
      }
    }

    const styles = StyleSheet.create({
      container: {
        flex: 1,
      },
    })

I get a syntax error Unexpected token var ->man = new Object(). How do I fix this?

Upvotes: 0

Views: 25006

Answers (2)

Hamdi Rhibi
Hamdi Rhibi

Reputation: 11

Try to create your object in typescript file(object.ts) for exemple:

 export class object {
   name: 'joe',
   age: 27,
   country: 'France'

   constructor(obj) {
     this.name=obj.key; 
     this.age=obj.nom; 
     this.country=obj.prenom; 
   }       
 }

Upvotes: 1

Billy Koswara
Billy Koswara

Reputation: 497

the wrong part is you export default class object, FYI: CLASS is not OBJECT

the correct using of export is

export const object = {
    name: 'joe',
    age: 27,
    country: 'France'
}

then you can import it with

import {object} from './object.js'

Upvotes: 0

Related Questions