Adding a JSON file into my table using a button - javascript

i have been import JSON in my JS file, and i want to display it using a button in my table but, the JSON file its a list of users, and i want to add one user on each click.
I've been trying so many codes with no success, i got to see that something happend in my console.log() but i never got to see it on the web.
i hope the question its clear to understand..
Does anybody have an idea??
my code:
imports + JSON
import React, { Component } from 'react';
import InitialData from '../../JSON/InitialData';
import AdditionalData from '../../JSON/AdditionalData';
const { supportRequest } = InitialData;
const { users } = AdditionalData;
class SupportRequest extends Component {
state = {
text: "Send",
users: [
{
name: "Bob Sacamento",
email: "bob_sacamento#gottlieb.ca",
timestamp: "2012-04-23T11:06:43.511Z",
phoneNumber: "214-300-5846",
city: "Long Island",
status: "unsent"
},
{
name: "Hal Kitzmiller",
email: "hal_kitzmiller#lindgren.com",
timestamp: "2012-04-23T08:22:43.511Z",
phoneNumber: "057-812-4000",
city: "The Bronx",
status: "unsent"
},
{
name: "Bob Cobb",
email: "bob.cobb#nelson.tv",
timestamp: "2012-04-23T14:22:43.511Z",
phoneNumber: "866-668-0327",
city: "Florence",
status: "unsent"
},
{
name: "Mike Moffett",
email: "mike_moffett#kaia.org",
timestamp: "2012-04-23T07:22:43.511Z",
phoneNumber: "647-851-1333",
city: "Upper East Side",
status: "unsent"
}
]
}
the Add button:
addUser = () => {
const array = this.state.users;
array.map((arr) => {
return (users.push({ users: arr[0] })
)
})
}
render() {
return (
<div>
<div style={{ fontSize: "25px" }} className="jsonData-container">Support Request</div>
<button
style={{ variant: "contained", alignContent: "flex-end" }}
onClick={() => this.addUser
}
>
Add
</button>
Rest of the code:
<div className="container" ></div>
<table className="table table-bordered table-striped table-hover">
<thead style={{ backgroundColor: "black", color: "white" }}>
<tr>
<th>name</th>
<th>email</th>
<th>timestamp</th>
<th>phoneNumber</th>
<th>city</th>
<th>status</th>
</tr>
</thead>
<tbody>
{supportRequest.map((supportRequest, index) => {
return (
<tr key={index}>
<td >
{supportRequest.name}
</td>
<td>
{supportRequest.email}
</td>
<td>
{supportRequest.timestamp}
</td>
<td>
{supportRequest.phoneNumber}
</td>
<td>
{supportRequest.city}
</td>
<td>
<button
onClick={() => {
this.setState({ text: 'send' })
console.log(this.setState({}))
}}>
{supportRequest.status}
</button>
</td>
</tr>
)
})}
</tbody>
</table>
</div>
)
}
}
export default SupportRequest;

you can use React.Children from React React.Children. As you have already retrieved the user data, you don't have to create new list of users on each click. You can simply control the view.
class App extends Component {
state = {
text: "Send",
users: [
{
name: "Bob Sacamento",
email: "bob_sacamento#gottlieb.ca",
timestamp: "2012-04-23T11:06:43.511Z",
phoneNumber: "214-300-5846",
city: "Long Island",
status: "unsent",
},
{
name: "Hal Kitzmiller",
email: "hal_kitzmiller#lindgren.com",
timestamp: "2012-04-23T08:22:43.511Z",
phoneNumber: "057-812-4000",
city: "The Bronx",
status: "unsent",
},
{
name: "Bob Cobb",
email: "bob.cobb#nelson.tv",
timestamp: "2012-04-23T14:22:43.511Z",
phoneNumber: "866-668-0327",
city: "Florence",
status: "unsent",
},
{
name: "Mike Moffett",
email: "mike_moffett#kaia.org",
timestamp: "2012-04-23T07:22:43.511Z",
phoneNumber: "647-851-1333",
city: "Upper East Side",
status: "unsent",
},
],
usersToShow: 3,
};
onLoadMore = () => {
this.setState({
usersToShow: this.state.usersToShow + 1,
});
};
onViewLess = () => {
this.setState({
usersToShow: this.state.usersToShow - 1,
});
};
showUerData = () =>
this.state.users.map((user, i) => <p key={i}>{user.name}</p>);
render() {
return (
<div className="App">
{React.Children.toArray(this.showUerData()).slice(
0,
this.state.usersToShow
)}
{this.state.usersToShow === this.state.users.length ? (
<button onClick={this.onViewLess}>View Less</button>
) : (
<button onClick={this.onLoadMore}>Load More</button>
)}
</div>
);
}
}

Related

How to push data in the table to axios.post

I am trying to post the data in the table but I keep getting the following in my console.log testing, undefined. What am I missing? or is there a better way to get the data to axios.post?
export default {
props: {
table: {
type:Array,
required: true,
default: null
}
},
data() {
},
methods: {
submit(item) {
axios.post('/newitem/${netId}', item)
.then(response => {
console.log("testing", response);
})
.catch(error => {
// handle the error
});
}
}
}
<table>
<tr v-for="(item, index) in table" :key="index">
<td> {{ item.name }} </td>
<td> {{ item.source }} </td>
<td> {{ item.accountId }} </td>
</tr>
</table>
<div>
<b-button #click="submit(item)">add</b-button>
</div>
example of data:
table: [{
name: 'John',
source: 'john#example.com',
accountId: 30
},
{
name: 'Sam',
source: 'sam#example.com',
accountId: 25
},
]

Display React Bootstrap's Modal when you press a button

Goal:
Display react bootstrap's modal when you press the button 'Open Modal'
Problem:
I do not know how to make it to show bootstrap's modal when I press the button 'Open Modal'
What part am I missing?
Stackblitz:
https://stackblitz.com/edit/react-bootstrap-examples-suktpo?
Info:
*I'm newbie in Reactjs
Thank you!
index.html
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<div id="root"></div>
index.js
import React, { Component } from 'react';
import { render } from 'react-dom';
import './style.css';
import DisplayModalContent from './DisplayModalContent';
import { Modal, Button } from 'react-bootstrap';
class App extends Component {
constructor() {
super();
this.state = {
openItem: null,
items: [
{
firstName: 'Josef',
lastName: 'Anderson',
key: 'josef.anderson',
startYear: 2021,
startMonth: 2
},
{
firstName: 'Jim',
lastName: 'West',
key: 'jim.west',
startYear: 2020,
startMonth: 3
},
{
firstName: 'Joe',
lastName: 'West',
key: 'joe.west',
startYear: 1998,
startMonth: 10
}
],
firstName: '',
lastName: ''
};
}
handleOpenModal = openItem => {
this.setState({ openItem });
};
handleCloseModal = () => {
this.setState({ openItem: null });
};
handleOpenItemValue = e => {
let { name, value } = e.target;
this.setState({
openItem: { ...this.state.openItem, [name]: value }
});
};
handleSubmit = () => {
console.log(document.getElementsByName('startMonth')[0].value);
alert(
JSON.stringify({
test: document.getElementsByName('startMonth')[0].value
})
);
};
render() {
const { items, openItem } = this.state;
return (
<div>
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th />
</tr>
</thead>
<tbody>
{items.map(item => {
const { firstName, lastName, key } = item;
return (
<tr key={key}>
<td>{firstName}</td>
<td>{lastName}</td>
<td>
<button onClick={() => this.handleOpenModal(item)}>
Open Modal
</button>
</td>
</tr>
);
})}
</tbody>
</table>
<DisplayModalContent item={openItem} />
</div>
);
}
}
render(<App />, document.getElementById('root'));
DisplayModalContent.js
import React, { Component } from 'react';
import { render } from 'react-dom';
import './style.css';
import { Modal, Button } from 'react-bootstrap';
const options = [
{ value: 1, label: 'Jan' },
{ value: 2, label: 'Feb' },
{ value: 3, label: 'Mars' },
{ value: 4, label: 'April' },
{ value: 5, label: 'May' },
{ value: 6, label: 'June' },
{ value: 7, label: 'July' },
{ value: 8, label: 'August' },
{ value: 9, label: 'Sept' },
{ value: 10, label: 'Oct' },
{ value: 11, label: 'Nov' },
{ value: 12, label: 'Dec' }
];
class DisplayModalContent extends Component {
constructor() {
super();
this.state = {
openItem: null,
firstName: '',
lastName: ''
};
}
componentDidUpdate(s) {
if (JSON.stringify(this.props) !== JSON.stringify(s)) {
this.setState({ openItem: this.props.item });
}
}
handleOpenModal = openItem => {
this.setState({ openItem });
};
handleCloseModal = () => {
this.setState({ openItem: null });
};
handleOpenItemValue = e => {
let { name, value } = e.target;
this.setState({
openItem: { ...this.state.openItem, [name]: value }
});
};
handleSubmit = () => {
console.log(document.getElementsByName('startMonth')[0].value);
alert(
JSON.stringify({
test: document.getElementsByName('startMonth')[0].value
})
);
};
hideShowModal = () => {
this.setState({ isModalOpen: !this.state.isModalOpen });
};
render() {
const { items, openItem } = this.state;
return (
<div>
{openItem !== null && (
<div isOpen={true}>
<Button variant="primary" onClick={() => this.hideShowModal()}>
Click to hide/show
</Button>
<Modal
show={this.state.isModalOpen}
onHide={() => this.hideShowModal()}
>
<Modal.Header closeButton>
<Modal.Title>This is modal title</Modal.Title>
</Modal.Header>
<Modal.Body>
<p>
First Name:
<br />
<input
type="text"
id="firstName"
name="firstName"
value={openItem.firstName}
onChange={e => this.handleOpenItemValue(e)}
/>
<input
type="text"
id="lastName"
name="lastName"
value={openItem.lastName}
onChange={e => this.handleOpenItemValue(e)}
/>
</p>
</Modal.Body>
<Modal.Footer>
<Button variant="secondary">CLOSE</Button>
<Button variant="primary">SAVE</Button>
</Modal.Footer>
</Modal>
</div>
)}
</div>
);
}
}
export default DisplayModalContent;
you don't have to use the state in the two components, just define the state in the parent component and pass it as a prop to the child component.
index.js
import React, { Component } from "react";
import DisplayModalContent from "./DisplayModalContent";
import { Modal, Button } from "react-bootstrap";
const items = [
{
firstName: "Josef",
lastName: "Anderson",
key: "josef.anderson",
startYear: 2021,
startMonth: 2
},
{
firstName: "Jim",
lastName: "West",
key: "jim.west",
startYear: 2020,
startMonth: 3
},
{
firstName: "Joe",
lastName: "West",
key: "joe.west",
startYear: 1998,
startMonth: 10
}
];
class App extends Component {
constructor() {
super();
this.state = {
open: false,
openItem: null,
items: [],
firstName: "",
lastName: ""
};
}
componentDidMount() {
this.setState({ items });
}
handleOpenModal = (openItem) => {
this.setState({ openItem, open: true });
};
handleCloseModal = () => {
this.setState({ open: false });
};
handleOpenItemValue = (e) => {
let { name, value } = e.target;
this.setState({
openItem: { ...this.state.openItem, [name]: value }
});
};
handleSubmit = (key) => {
const { openItem, items } = this.state;
const updatedItems = items.filter((i) => i.key !== key);
this.setState({
open: false,
items: [...updatedItems, openItem]
});
// console.log(document.getElementsByName("startMonth")[0].value);
// alert(
// JSON.stringify({
// test: document.getElementsByName("startMonth")[0].value
// })
// );
};
render() {
const { items, openItem, open } = this.state;
return (
<div>
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th />
</tr>
</thead>
<tbody>
{items &&
items.map((item) => {
const { firstName, lastName, key } = item;
return (
<tr key={key}>
<td>{firstName}</td>
<td>{lastName}</td>
<td>
<button onClick={() => this.handleOpenModal(item)}>
Open Modal
</button>
</td>
</tr>
);
})}
</tbody>
</table>
<DisplayModalContent
item={openItem}
isOpen={open}
onClose={this.handleCloseModal}
onSubmit={this.handleSubmit}
handleOpenItemValue={(e) => this.handleOpenItemValue(e)}
/>
<br />
<div> {JSON.stringify(items)} </div>
</div>
);
}
}
export default App;
DisplayModalContent.js
import React, { Component } from "react";
import { Modal, Button } from "react-bootstrap";
const options = [
{ value: 1, label: "Jan" },
{ value: 2, label: "Feb" },
{ value: 3, label: "Mars" },
{ value: 4, label: "April" },
{ value: 5, label: "May" },
{ value: 6, label: "June" },
{ value: 7, label: "July" },
{ value: 8, label: "August" },
{ value: 9, label: "Sept" },
{ value: 10, label: "Oct" },
{ value: 11, label: "Nov" },
{ value: 12, label: "Dec" }
];
class DisplayModalContent extends Component {
render() {
const { item, isOpen, onClose, handleOpenItemValue, onSubmit } = this.props;
return (
<div>
<div>
<Modal show={isOpen} onHide={onClose}>
<Modal.Header closeButton>
<Modal.Title>This is modal title</Modal.Title>
</Modal.Header>
<Modal.Body>
<p>
First Name:
<br />
<input
type="text"
id="firstName"
name="firstName"
value={item && item.firstName}
onChange={(e) => handleOpenItemValue(e)}
/>
<input
type="text"
id="lastName"
name="lastName"
value={item && item.lastName}
onChange={(e) => handleOpenItemValue(e)}
/>
</p>
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={onClose}>
CLOSE
</Button>
<Button variant="primary" onClick={() => onSubmit(item.key)}>
SAVE
</Button>
</Modal.Footer>
</Modal>
</div>
</div>
);
}
}
export default DisplayModalContent;
Live working Demo
I was reviewing your code and I see you're using this.state.isModalOpen to show the modal, but in the DisplayModalContent component that state is never updated when you're receiving the props from your index. So, the easy fix for that is update isModalOpen when you are receiving the props. However, from my experience I would recommend do not use a new state to handle the modal open state in the DisplayModalContent component, you can use it directly from the props:
<DisplayModalContent
item={openItem}
isOpen={!!openItem}
onClose={this.handleCloseModal}
/>
and in your DisplayModalContent component, you can replace any local state and in order to use the component props:
...
<Modal
show={this.props.isOpen}
onHide={this.props.onClose}
>
...
By doing this your code will be more simple and readable.

TypeError: Cannot convert undefined or null to object in react

I am trying to render a simple HTML table to the screen using the following code:
import { useEffect, useState } from "react";
import "./App.css";
import { getAllBooks } from "./services/bookService";
function App() {
const [books, setBooks] = useState([]);
useEffect(() => {
const loadBooks = async () => {
try {
const response = await getAllBooks();
console.log(response.data.books);
setBooks(response.data.books);
} catch (error) {
console.log(error);
}
};
loadBooks();
}, []);
return (
<div className="container">
<h1>Simple Inventory Table</h1>
<table>
<thead>
<tr>
<th></th>
{Object.keys(books[0]).map((item) => {
return <th key={item}>{item}</th>;
})}
</tr>
</thead>
</table>
</div>
);
}
export default App;
The array of books I get looks like this :
const books = [
{
id: 1,
Title: "Book One",
Stock: 4,
ISBN: "9874223457654",
ImageURL: null,
Pages: 281,
createdAt: "2021-04-30T12:57:52.000Z",
updatedAt: "2021-04-30T13:43:07.000Z",
AuthorId: 1,
GenreId: 2
},
{
id: 2,
Title: "Book Two",
Stock: 5,
ISBN: "9825324716432",
ImageURL: null,
Pages: 231,
createdAt: "2021-04-30T12:57:52.000Z",
updatedAt: "2021-04-30T12:57:52.000Z",
AuthorId: 3,
GenreId: 6
}
];
But instead of a row of columns, I get the error: TypeError: Cannot convert undefined or null to object.
This is confusing to me because I have tried getting keys from the output array using Object.keys(books[0]) and it worked, Link to JS Bin. Can someone help me out with this?
Because the data would not be loaded at the time of rendering. Which will leads to an exception wile trying to access books[0]
Write a condition like this,
{books.length>0&&
<tr>
<th></th>
{Object.keys(books[0]).map((item) => {
return <th key={item}>{item}</th>;
})}
</tr>
}
Export the books array from booksService.js to display all the keys
import { useEffect, useState } from "react";
import { getAllBooks } from "./bookService";
function App() {
const [books, setBooks] = useState([]);
useEffect(() => {
const loadBooks = async () => {
try {
const response = await getAllBooks();
console.log(response);
setBooks(response);
} catch (error) {
console.log(error);
}
};
loadBooks();
}, []);
return (
<div className="container">
<h1>Simple Inventory Table</h1>
<table>
<thead>
{books.length > 0 && (
<tr>
<th></th>
{Object.keys(books[0]).map((item) => {
return <th key={item}>{item}</th>;
})}
</tr>
)}
</thead>
</table>
</div>
);
}
export default App;
The bookService.js looks like this:
const books = [
{
id: 1,
Title: "Book One",
Stock: 4,
ISBN: "9874223457654",
ImageURL: null,
Pages: 281,
createdAt: "2021-04-30T12:57:52.000Z",
updatedAt: "2021-04-30T13:43:07.000Z",
AuthorId: 1,
GenreId: 2
},
{
id: 2,
Title: "Book Two",
Stock: 5,
ISBN: "9825324716432",
ImageURL: null,
Pages: 231,
createdAt: "2021-04-30T12:57:52.000Z",
updatedAt: "2021-04-30T12:57:52.000Z",
AuthorId: 3,
GenreId: 6
}
];
async function getAllBooks() {
return books;
}
module.exports = {
getAllBooks
};

ReactJS setState doesnt update anything

Im quite new to reactJS and ive been trying to create a table sorting based on this https://www.florin-pop.com/blog/2019/07/sort-table-data-with-react/. All works fine, did some changes and all, except my setState doesnt seem to update the my sorting. Here are some of my codes:
class TableView extends Component {
constructor(props) {
super(props)
this.state = {
students: [
{ name: 'Brian', age: 20, email: 'brian#hotmail.com' },
{ name: 'Kathy', age: 42, email: 'kathy#gmail.com' },
{ name: 'Donney', age: 50, email: 'donneylee#sjks.com' },
{ name: 'Cindy', age: 36, email: 'cindy#jflasjfl.com'}
]
}
this.headers = ['name', 'age', 'email'];
this.sortTypes = {
up: {
fn: (a, b) => { if (a.name < b.name) { return - 1 }}
},
down: {
fn: (a, b) => { if (a.name > b.name) { return 1 }}
},
default: {
fn: (a, b) => a
}
};
this.stateSort = {
currentSort: 'default'
};
}
onSortAsc = () => {
const { currentSort } = this.stateSort;
this.setState({ currentSort: 'up' }, () => {
console.log(currentSort);
});
}
renderTableData() {
const { currentSort } = this.stateSort;
const data = this.state.students;
return (
( data.length > 0 && (
[...data].sort(this.sortTypes[currentSort].fn).map(p => (
<tr key={p.name}>
<td>{p.name}</td>
<td>{p.age}</td>
<td>{p.email}</td>
</tr>
))
)
))
}
renderTableHeader() {
return<th key={index}>{key}<DropdownButton menuAlign="right" className="table-dropdown" title="">
<Dropdown.Item onClick={this.onSortAsc}>Sort A-Z</Dropdown.Item>
<Dropdown.Item href="#/action-2">Sort Z-A</Dropdown.Item>
</DropdownButton></th>
}
//The table
render() { //Whenever our class runs, render method will be called automatically, it may have already defined in the constructor behind the scene.
return (
<div>
<div id="table-view">
<button>Hide Fields</button>
<span class="more-btn"><FaEllipsisH /></span>
<div id="table-content">
<table id='funding-stage'>
<thead>
<tbody>
<tr>{this.renderTableHeader()}</tr>
{this.renderTableData()}
</tbody>
</thead>
</table>
</div>
</div>
</div>
)
}
};
export default Tableview;
Im not sure what went wrong. Ive tried different solutions but nothing seems to work.
The currentSort state should be in the state object (e.g you should access it as this.state.currentSort ) if you put it in another object ( stateSort ) and that object is the one in the state object, changing it will not cause update or rerender.
1 - remove
this.stateSort = {
currentSort: 'default'
};
2 - add currentSort to state object
this.state = {
students: [
{ name: 'Brian', age: 20, email: 'brian#hotmail.com' },
{ name: 'Kathy', age: 42, email: 'kathy#gmail.com' },
{ name: 'Donney', age: 50, email: 'donneylee#sjks.com' },
{ name: 'Cindy', age: 36, email: 'cindy#jflasjfl.com'}
],
currentSort: 'default'
}
3 - in onSortAsc function
onSortAsc = () => {
this.setState({ currentSort: 'up' }, () => {
console.log(currentSort);
});
}
and If that didn't help, update your code snippet and show the whole component to help you better.

.setState(): Filtering a person from an array of objects on button click

In the code below, I am trying to remove a person from what will eventually be an org chart when the delete button next to their name is clicked. At the moment, nothing happens. The closest I've come is all 5 people being deleted when any one of the delete buttons is clicked, but I only want the one person deleted who's button is clicked. I feel like I'm making more of a JS error than a React error.
See the full code sandbox here.
Any help would be appreciated, thank you!
import React from "react";
import { Component } from "react";
const list = [
{
name: "Person 1",
phone: "123-4567",
itemId: 11
},
{
name: "Person 2",
phone: "123-4567",
itemId: 12
},
{
name: "Person 3",
phone: "123-4567",
itemId: 23
},
{
name: "Person 4",
phone: "123-4567",
itemId: 34
},
{
name: "Person 5",
phone: "123-4567",
itemId: 45
}
];
class Entry extends Component {
constructor(props) {
super(props);
this.state = {
list: list
};
this.handleClick = this.handleClick.bind(this);
}
handleClick(e) {
this.setState({
list: this.state.list.filter(function(person) {
return person !== e.target.value;
})
});
}
render() {
return (
<div>
{this.state.list.map(item =>
<tr key={item.itemId}>
<td>
{item.name}
</td>
<td>
{item.phone}
</td>
<td>
<a className="delete" onClick={this.handleClick} />
</td>
</tr>
)}
</div>
);
}
}
export default Entry;
Your click event has no value, you can pass the itemId:
onClick={() => this.handleClick(item.itemId)}
Then your handleClick should look like:
handleClick(itemId) {
this.setState({
list: this.state.list.filter(function(person) {
return person.itemId !== itemId;
})
});
}
https://codesandbox.io/s/mo2l8z7469
Both the above solution violates one of the best practices or I should say essential practices of react, that we should use property initializer syntax, which is passing the function defined above instead of passing an arrow function inside prop you can read it here in last paragraph of https://facebook.github.io/react/docs/handling-events.html
class Entry extends Component {
/* your handler method */
handleDelete(itemId){
return () => {
this.setState({
/* logic to filter deleted item from old state */
});
}
}
/* render method */
render() {
return (
<div>
{/* passing onDelete callback */}
<a className="delete" onClick={this.handleClick(item.id)} />
</div>
)
}
}
//import React from 'react';
//import ReactDOM from 'react-dom';
const list = [
{
name: "Person 1",
phone: "123-4567",
itemId: 11
},
{
name: "Person 2",
phone: "123-4567",
itemId: 12
},
{
name: "Person 3",
phone: "123-4567",
itemId: 23
},
{
name: "Person 4",
phone: "123-4567",
itemId: 34
},
{
name: "Person 5",
phone: "123-4567",
itemId: 45
}
];
class Entry extends React.Component {
constructor(props) {
super(props);
this.state = {
list: list
};
this.handleClick = this.handleClick.bind(this);
}
handleClick(item) {
let filterList = this.state.list.filter((user) => {
if(user.itemId === item.itemId) {
return false;
}
return true;
})
this.setState({
list: filterList
});
}
render() {
return (
<div>
<table>
<tbody>
{this.state.list.map(item =>
<tr key={item.itemId}>
<td>
{item.name}
</td>
<td>
{item.phone}
</td>
<td>
<button className="delete" onClick={() => this.handleClick(item)} >Del</button>
</td>
</tr>
)}
</tbody>
</table>
</div>
);
}
}
ReactDOM.render(
<Entry />,
document.getElementById('app')
);
delete
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

Categories