admin管理员组文章数量:1391975
I am trying to use bootstrap modal to display each item on a table when clicked on the view button. I initially did it in a single file and it's but now I tried to separate the modal from the list of items both which means I have two files for them now and I am getting this error: TypeError: Cannot read properties of undefined (reading 'name')
Below is my code: ModalPractice.js
import React, {useState} from 'react';
import { Table } from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
import { DetailModal } from './DetailModal';
const ModalPractice = () => {
const [show, setShow] = useState(false);
const [selectedItem, setSelectedItem] = useState({});
const handleClose = () => {
setShow(false);
setSelectedItem({});
};
const handleShow = (e, item) => {
setShow(true);
setSelectedItem(item);
};
const food = [
{
id: 1,
name: 'rice',
category: 'grain',
image: 'images/rice.jpg',
},
{
id: 2,
name: 'beans',
category: 'grain and protein',
image: 'images/beans.jpg',
},
{
id: 3,
name: 'amala',
category: 'swallow',
image: 'images/amala.jpg',
},
{
id: 4,
name: 'Oat',
category: 'cereals',
image: 'images/oat.jpg',
},
{
id: 5,
name: 'coke',
category: 'soft drink',
image: 'images/coke.jpg',
},
{
id: 6,
name: 'banana',
category: 'fruit',
image: 'images/banana.jpg',
},
{
id: 7,
name: 'okra',
category: 'vegetable',
image: 'images/okra.jpg',
},
{
id: 8,
name: 'yam',
category: 'tuber',
image: 'images/yam.jpg',
},
{
id: 9,
name: 'palm oil',
category: 'fat',
image: 'images/palmoil.jpg',
},
{
id: 10,
name: 'orange',
category: 'fruit',
image: 'images/orange.jpg',
},
];
return (
<div>
{/* <Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>{selectedItem.name}</Modal.Title>
</Modal.Header>
<Modal.Body>{selectedItem.category}</Modal.Body>
<Modal.Footer>
<Button variant='secondary' onClick={handleClose}>
Close
</Button>
<Button variant='primary' onClick={handleClose}>
Save Changes
</Button>
</Modal.Footer>
</Modal> */}
<Table striped bordered hover>
<thead>
<tr>
<th>#</th>
<th>Food Name</th>
<th>Food Category</th>
<th>Image</th>
</tr>
</thead>
<tbody>
{food.map((list) => (
<tr className='align-middle' key={list.id}>
<td>{list.id}</td>
<td>{list.name}</td>
<td>{list.category}</td>
<td>
<img alt='' src={list.image} width='100' height='100' />
</td>
<td>
<DetailModal
id={list.id}
name={list.name}
category={list.category}
handleShow={handleShow}
handleClose={handleClose}
show={show}
selectedItem={selectedItem}
/>
</td>
</tr>
))}
</tbody>
</Table>
</div>
);
};
export default ModalPractice;
DetailModal.js
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Button, Modal } from 'react-bootstrap';
export const DetailModal = (props) => {
const {selectedItem, show, handleClose, handleShow, list} = props
return (
<div>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>{selectedItem.name}</Modal.Title>
</Modal.Header>
<Modal.Body>{selectedItem.category}</Modal.Body>
<Modal.Footer>
<Button variant='secondary' onClick={handleClose}>
Close
</Button>
<Button variant='primary' onClick={handleClose}>
Save Changes
</Button>
</Modal.Footer>
</Modal>
<Button variant='primary' onClick={(e) => handleShow(e, list)}>
Detail
</Button>
{console.log(props)}
</div>
);
};
I am trying to use bootstrap modal to display each item on a table when clicked on the view button. I initially did it in a single file and it's but now I tried to separate the modal from the list of items both which means I have two files for them now and I am getting this error: TypeError: Cannot read properties of undefined (reading 'name')
Below is my code: ModalPractice.js
import React, {useState} from 'react';
import { Table } from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
import { DetailModal } from './DetailModal';
const ModalPractice = () => {
const [show, setShow] = useState(false);
const [selectedItem, setSelectedItem] = useState({});
const handleClose = () => {
setShow(false);
setSelectedItem({});
};
const handleShow = (e, item) => {
setShow(true);
setSelectedItem(item);
};
const food = [
{
id: 1,
name: 'rice',
category: 'grain',
image: 'images/rice.jpg',
},
{
id: 2,
name: 'beans',
category: 'grain and protein',
image: 'images/beans.jpg',
},
{
id: 3,
name: 'amala',
category: 'swallow',
image: 'images/amala.jpg',
},
{
id: 4,
name: 'Oat',
category: 'cereals',
image: 'images/oat.jpg',
},
{
id: 5,
name: 'coke',
category: 'soft drink',
image: 'images/coke.jpg',
},
{
id: 6,
name: 'banana',
category: 'fruit',
image: 'images/banana.jpg',
},
{
id: 7,
name: 'okra',
category: 'vegetable',
image: 'images/okra.jpg',
},
{
id: 8,
name: 'yam',
category: 'tuber',
image: 'images/yam.jpg',
},
{
id: 9,
name: 'palm oil',
category: 'fat',
image: 'images/palmoil.jpg',
},
{
id: 10,
name: 'orange',
category: 'fruit',
image: 'images/orange.jpg',
},
];
return (
<div>
{/* <Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>{selectedItem.name}</Modal.Title>
</Modal.Header>
<Modal.Body>{selectedItem.category}</Modal.Body>
<Modal.Footer>
<Button variant='secondary' onClick={handleClose}>
Close
</Button>
<Button variant='primary' onClick={handleClose}>
Save Changes
</Button>
</Modal.Footer>
</Modal> */}
<Table striped bordered hover>
<thead>
<tr>
<th>#</th>
<th>Food Name</th>
<th>Food Category</th>
<th>Image</th>
</tr>
</thead>
<tbody>
{food.map((list) => (
<tr className='align-middle' key={list.id}>
<td>{list.id}</td>
<td>{list.name}</td>
<td>{list.category}</td>
<td>
<img alt='' src={list.image} width='100' height='100' />
</td>
<td>
<DetailModal
id={list.id}
name={list.name}
category={list.category}
handleShow={handleShow}
handleClose={handleClose}
show={show}
selectedItem={selectedItem}
/>
</td>
</tr>
))}
</tbody>
</Table>
</div>
);
};
export default ModalPractice;
DetailModal.js
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Button, Modal } from 'react-bootstrap';
export const DetailModal = (props) => {
const {selectedItem, show, handleClose, handleShow, list} = props
return (
<div>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>{selectedItem.name}</Modal.Title>
</Modal.Header>
<Modal.Body>{selectedItem.category}</Modal.Body>
<Modal.Footer>
<Button variant='secondary' onClick={handleClose}>
Close
</Button>
<Button variant='primary' onClick={handleClose}>
Save Changes
</Button>
</Modal.Footer>
</Modal>
<Button variant='primary' onClick={(e) => handleShow(e, list)}>
Detail
</Button>
{console.log(props)}
</div>
);
};
Share
Improve this question
asked Sep 19, 2021 at 13:45
Abubakar OluyinkaAbubakar Oluyinka
3511 gold badge6 silver badges18 bronze badges
2
-
From quickly tracing through your code, I believe you have left out a
list
prop in theDetailModal
. I suspect you meant to passlist={list}
from the parent? – Robin Zigmond Commented Sep 19, 2021 at 13:56 - Yeah. Thanks. Just dicovered. – Abubakar Oluyinka Commented Sep 19, 2021 at 15:06
1 Answer
Reset to default 1Its showing undefined because you have not passed a list prop to the DetailModal
its should be like this
ModalPractice.js
import React, {useState} from 'react';
import { Table } from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
import { DetailModal } from './DetailModal';
const ModalPractice = () => {
const [show, setShow] = useState(false);
const [selectedItem, setSelectedItem] = useState({});
const handleClose = () => {
setShow(false);
setSelectedItem({});
};
const handleShow = (e, item) => {
setShow(true);
setSelectedItem(item);
};
const food = [
{
id: 1,
name: 'rice',
category: 'grain',
image: 'images/rice.jpg',
},
{
id: 2,
name: 'beans',
category: 'grain and protein',
image: 'images/beans.jpg',
},
{
id: 3,
name: 'amala',
category: 'swallow',
image: 'images/amala.jpg',
},
{
id: 4,
name: 'Oat',
category: 'cereals',
image: 'images/oat.jpg',
},
{
id: 5,
name: 'coke',
category: 'soft drink',
image: 'images/coke.jpg',
},
{
id: 6,
name: 'banana',
category: 'fruit',
image: 'images/banana.jpg',
},
{
id: 7,
name: 'okra',
category: 'vegetable',
image: 'images/okra.jpg',
},
{
id: 8,
name: 'yam',
category: 'tuber',
image: 'images/yam.jpg',
},
{
id: 9,
name: 'palm oil',
category: 'fat',
image: 'images/palmoil.jpg',
},
{
id: 10,
name: 'orange',
category: 'fruit',
image: 'images/orange.jpg',
},
];
return (
<div>
{/* <Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>{selectedItem.name}</Modal.Title>
</Modal.Header>
<Modal.Body>{selectedItem.category}</Modal.Body>
<Modal.Footer>
<Button variant='secondary' onClick={handleClose}>
Close
</Button>
<Button variant='primary' onClick={handleClose}>
Save Changes
</Button>
</Modal.Footer>
</Modal> */}
<Table striped bordered hover>
<thead>
<tr>
<th>#</th>
<th>Food Name</th>
<th>Food Category</th>
<th>Image</th>
</tr>
</thead>
<tbody>
{food.map((list) => (
<tr className='align-middle' key={list.id}>
<td>{list.id}</td>
<td>{list.name}</td>
<td>{list.category}</td>
<td>
<img alt='' src={list.image} width='100' height='100' />
</td>
<td>
<DetailModal
id={list.id}
name={list.name}
category={list.category}
handleShow={handleShow}
handleClose={handleClose}
show={show}
list={list}
selectedItem={selectedItem}
/>
</td>
</tr>
))}
</tbody>
</Table>
</div>
);
};
export default ModalPractice;
本文标签: javascriptTypeError Cannot read properties of undefined (reading 39name39)Stack Overflow
版权声明:本文标题:javascript - TypeError: Cannot read properties of undefined (reading 'name') - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744701710a2620600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论