-1

How Can I include a Custom JavaScript in ReactJS and use Array of Object in that? I am trying to display a list of songs. So, I have created a file with the name songList.js and inside that, I have created an Array of objects how can I include it in my list.js file and use it inside that?

songList.js Code :-

const songList = [{
        name: "All We Know",
        artist: "The Chainsmokers ft. Phoebe Ryan",
        album: "All We Know",
    },
    {
        name: "Alone",
        artist: "Alan Walker",
        album: "Different World",
    },
    {
        name: "Be Kind",
        artist: "Marshmello & Halsey",
        album: "Be Kind",
    },
    {
        name: "Beach House",
        artist: "The Chainsmokers",
        album: "Sick Boy",
    },
];

I am trying to do this but it is not working please tell me where I am wrong?

list.js code

import React, { Component } from 'react'
import './songList.js'

export class list extends Component {
    render() {
        return (
            <div>
                {songList[0].name}
            </div>
        )
    }
}

export default list
1
  • You need to export the list (using export default songList), then import it using import songList from './songList.js' Commented Nov 4, 2020 at 14:10

1 Answer 1

1

First, you export the object, then import it correctly:

export const songList = ...

import { songList } from './songList.js';
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.