0

I am doing a React course here and the starter files keep giving me this error:

TypeError: Cannot read property '0' of undefined

From this starter code:

import React from 'react'
import ReactDOM from 'react-dom'

const notes = [
  {
    id: 1,
    content: 'HTML is easy',
    date: '2019-05-30T17:30:31.098Z',
    important: true
  },
  {
    id: 2,
    content: 'Browser can execute only Javascript',
    date: '2019-05-30T18:39:34.091Z',
    important: false
  },
  {
    id: 3,
    content: 'GET and POST are the most important methods of HTTP protocol',
    date: '2019-05-30T19:20:14.298Z',
    important: true
  }
]

const App = (props) => {
  const { notes } = props

  return (
    <div>
      <h1>Notes</h1>
      <ul>
        <li>{notes[0].content}</li>
        <li>{notes[1].content}</li>
        <li>{notes[2].content}</li>
      </ul>
    </div>
  )
}

ReactDOM.render(
  <App notes={notes} />,
  document.getElementById('root')
)

Above code shows:

Attempted import error: './App' does not contain a default export (imported as 'App')

I tried amending by adding export default app and received:

TypeError: Cannot read property '0' of undefined

How can I solve this problem?

15
  • Looks like you are expecting notes that's declared outside your component to be the same as the one you are using inside, but can't tell unless you share the code where you are using App Commented Oct 4, 2019 at 1:05
  • Yes this code is from App. used the create react app boiler plate Commented Oct 4, 2019 at 1:13
  • How you are passing notes as props to App component? Commented Oct 4, 2019 at 1:15
  • ReactDOM.render( <App notes={notes} />, document.getElementById('root') ) Commented Oct 4, 2019 at 1:18
  • Is this in another file? Commented Oct 4, 2019 at 1:19

1 Answer 1

1

The code from link you are referring is the complete index.js file only.

If you want your code to be modular and want to separate out App component, in that case, you should do this,

index.js

import React, { Component } from 'react';
import { render } from 'react-dom';
import App from "./App";   //import your component (imported without { }, because App component exported as default)

const notes = [
  {
    id: 1,
    content: 'HTML is easy',
    date: '2019-05-30T17:30:31.098Z',
    important: true
  },
  {
    id: 2,
    content: 'Browser can execute only Javascript',
    date: '2019-05-30T18:39:34.091Z',
    important: false
  },
  {
    id: 3,
    content: 'GET and POST are the most important methods of HTTP protocol',
    date: '2019-05-30T19:20:14.298Z',
    important: true
  }
]

//Pass the props here
render(<App notes={notes}/>, document.getElementById('root'));

App.js

import React from 'react'

const App = (props) => {
  const { notes } = props

  return (
    <div>
      <h1>Notes</h1>
      <ul>
        <li>{notes[0].content}</li>
        <li>{notes[1].content}</li>
        <li>{notes[2].content}</li>
      </ul>
    </div>
  )
}

export default App

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

I would improve upon this by using .map() instead of the multiple list items. That way if you add to the number of items in your notes you don't have to worry about changing the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.