How can i solve this issue ? TypeError: Cannot read properties of undefined (reading 'map') in react.js

Viewed 447

Hierarchy Structure Picture : https://prnt.sc/1y2ply6

notes.js(file) :

const notes = [
    {
      _id: "1",
      title: "Day 1 of college",
      content:
        "I made a few new friends and introduced myself to a lot of new teachers.",
      category: "College",
    },
    {
      _id: "2",
      title: "Learned some Node JS",
      content: "Learned how to create a server in node JS and my first API",
      category: "Learning",
    },
    {
      _id: "3",
      title: "Watched some Anime",
      content: "Finished 2 seasons of Attack on Titan and My Hero academia.",
      category: "Entertainment",
    },
    {
      _id: 4,
      title: "Started React JS",
      content:
        "Made my first App in React JS, feels awesome to learn something new. I aim to be a full stack dev someday",
      category: "Learning",
    },
  ];
  
  module.exports = notes;

MyNotes.js(file)

import React from 'react'
import { Link } from 'react-router-dom'
import { Accordion, Badge, Button, Card } from 'react-bootstrap'
import MainScreen from '../../components/MainScreen'
import { notes } from '../../data/notes.js'

const MyNotes = () => {

    const deleteHandler = (id) => {
        if(window.confirm("Are you sure?")){
             
        }
    }
    return (
        <MainScreen title='Welcome Deepak Sarkar'>
           <Link to="/createnote">
               <Button style={{ marginLeft: 10, marginBottom: 6, size:"lg"}}>
                   Create New Note
               </Button>
            </Link>
               {
                   notes.map((note) => (
                    <Accordion>
                        <Card style={{ margin: 10 }}>
                   <Card.Header style={{ display:"flex" }}>
                       <span style={{
                           color:"black",
                           textDecoration:"none",
                           flex:1,
                           cursor:"pointer",
                           alignSelf:"center",
                           fontSize:18
                       }}> 
                       <Accordion.Toggle as={Card.Text} variant='link' eventKey="0">
                           {note.title} 
                        </Accordion.Toggle>
                        </span>
                   <div>
                       <Button href={`/note/${note._id}`}>Edit</Button>
                       <Button 
                        variant='danger' 
                        className="mx-2"
                        onClick={() => deleteHandler(note._id)}>Delete</Button>
                   </div>
                    
                   </Card.Header>
                   <Accordion.Collapse eventKey="0">
                   <Card.Body>
                    <h4><Badge variant="success">
                        Category - {note.category} 
                    </Badge></h4>
                    <blockquote className="blockquote mb-0">
                  <p>{note.content}</p>
                 <footer className="blockquote-footer"> Created on -date </footer>
                </blockquote>
                </Card.Body>
                </Accordion.Collapse>
               </Card>
                    </Accordion>
                  
                   ))
               }
        </MainScreen>
    )
}

export default MyNotes

if i remove "{}" from import { notes } from '../../data/notes.js' and add export default notes in notes.js i get this error : Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of MyNotes.

can anyone help me solve this please ?

2 Answers

It seems you have mixed up which version of react-bootstrap you are using. The Accordion component API changed between v1 (Bootstrap 4.6) and v2 (Bootstrap 5.1).

Here with react-bootstrap v1.6.4 your code works without issue.

Edit how-can-i-solve-this-issue-typeerror-cannot-read-properties-of-undefined-rea

V1 Accordion API

This version has the Accordion, Accordion.Toggle, and Accordion.Collapse components.

V2 Accordion API

The v2 version has a few more options with Accordion, Accordion.Item, Accordion.Header, Accordion.Body, Accordion.Button, and Accordion.Collapse.

I suggest sticking to v1.6.4 and keeping your existing code working. If you want, or need, to upgrade then it's a bit unclear what immediate component changes you'll need to have a similarly working UI/UX, though I suspect the new accordion components will replace some of the card components currently used.

You can use either named or default export of the notes though, just don't use the module.exports.

The problem I have find is about the import you do of your notes.js file. Using {} you try to get a "notes" object inside your "notes" array. I have tested it, without {} and leaving your notes.js file as you have it, and work for me.

 import notes from '../../data/notes.js'

I hope it can solve your problem.

Related