Charlinhovdb
Charlinhovdb

Reputation: 53

Trying to access data in the movies file and use it in the home file

I am trying to access data in the movies const in the movies.js file and use the data in my home.js file. I tried importing it but it's not working. Anyone that could have a look?

My home file (where:

import React, { PureComponent } from 'react'
import movies$ from "./movies"

class Home extends PureComponent {
    constructor(props) {
        super(props)

        this.state = {

        }
    }

    render() {
        return (
            // <div>Charles</div>
            <div>{this.movies$[0].title}</div>
        )
    }
}

export default Home

const movies = [
  {
    id: '1',
    title: 'Oceans 8',
    category: 'Comedy',
    likes: 4,
    dislikes: 1
  }, {
    id: '2',
    title: 'Midnight Sun',
    category: 'Comedy',
    likes: 2,
    dislikes: 0
  }, {
    id: '6',
    title: 'Pulp Fiction',
    category: 'Thriller',
    likes: 11,
    dislikes: 3
  }
]

export const movies$ = new Promise((resolve, reject) => setTimeout(resolve, 100, movies))

Upvotes: 0

Views: 40

Answers (1)

Umair Farooq
Umair Farooq

Reputation: 1823

In movies .js:

export const movies = [
{
  id: '1',
  title: 'Oceans 8',
  category: 'Comedy',
  likes: 4,
  dislikes: 1
}, {
  id: '2',
  title: 'Midnight Sun',
  category: 'Comedy',
  likes: 2,
  dislikes: 0
}, {
  id: '6',
  title: 'Pulp Fiction',
  category: 'Thriller',
  likes: 11,
  dislikes: 3
 }
];

In home.js

import { movies } from './movies.js';
// in component
<div>{movies[0].title}</div>

Upvotes: 1

Related Questions