I want to filter list by alphabet and search input. On click of each alphabet it should sort the list according to the alphabet.
Attached is the image to explain better
For example : If you click on "A" then it will display list starting with "A" Like ( Apple, Air plane, Adidas ) etc. And the case should be same when you input in search input box.
Below is the render function in my component which is fetching the list by JSON API
component.js
{this.props.items.list.map((item) => (
<li key={item.id} className="celeb-item">
<div className="celeb-item-info">
<img src={item.image_url} className="img-responsive" />
<strong>{item.name}</strong>
</div>
</li>
))}
import React, { Component } from 'react';
class App extends Component {
state = { searchInput: '', alphabet: ''};
onSearchInputChange = (e) => {
this.setState({searchInput: e.target.value})
}
onAlphabetClick = (e) => {
this.setState({alphabet: e.target.value})
}
prepareAlphabets = () => {
let result = [];
for(let i=65; i<91; i++) {
result.push(
<button type="button" key={i} onClick={this.onAlphabetClick} value={String.fromCharCode(i)} >{String.fromCharCode(i)}</button>
)
}
return result;
}
elementContainsSearchString = (searchInput, element) => (searchInput ? element.name.toLowerCase().includes(searchInput.toLowerCase()) : false);
filterItems = (itemList) => {
let result = [];
const { searchInput,alphabet } = this.state;
if(itemList && (searchInput || alphabet)) {
result = itemList.filter((element) => (element.name.charAt(0).toLowerCase() === alphabet.toLowerCase()) ||
this.elementContainsSearchString(searchInput, element));
} else {
result = itemList || [];
}
result = result.map((item)=> (<li>{item.name}</li>))
return result;
}
render() {
const itemList = [{id: 1, name:'abcd'},{id: 2, name:'gfhj'}, {id: 3, name:'fh'}, {id: 4, name:'zxbv'}, {id: 5, name:'ewyur'}, {id: 6, name:'gsdjhbndf'}, {id: 7, name:'gbhfvd'}, {id: 8, name:'wgtaqe'}, {id: 1, name:'ab'}, {id: 1, name:'bcd'}, {id: 1, name:'cde'}];
// const itemList = undefined;
const filteredList = this.filterItems(itemList);
return (
<div>
<input type="search" onChange={this.onSearchInputChange} />
{this.prepareAlphabets()}
<ul>
{filteredList}
</ul>
</div>
);
}
}
export default App;
The most straightforward way to achieve what you describe is to chain an Array.filter just before your Array.map.
You can use the onChange & onClick handles on your search field and alphabet buttons, respectively, to modify a parameter kept in your components state. This parameter could then be passed into your filter, looking something like this:
{this.props.items.list
.filter(item => item.name.startsWith(this.state.searchValue)
.map(item => (
<li key={item.id} className="celeb-item">
<div className="celeb-item-info">
<a href="#">
<img src={item.image_url} className="img-responsive" />
</a>
<strong>{item.name}</strong>
</div>
</li>
))
}
Related
I am creating on drag and drop functionality using react. but my handleDrop function is not being invoked. It means my onDrop event is not working. not found any solution. thanks in advance.
I am sharing my code snippet with you guys, please have a look.:
import React, { useState, useRef } from 'react';
const App: React.FC = () => {
const [cards, setCards] = useState([
{ id: 1, title: 'Card 1', list: 'TODO' },
{ id: 2, title: 'Card 2', list: 'TODO' },
{ id: 3, title: 'Card 3', list: 'COMPLETED' },
{ id: 4, title: 'Card 4', list: 'COMPLETED' },
]);
const dragItem = useRef<HTMLDivElement>(null);
const [draggedOver, setDraggedOver] = useState<null | number>(null);
const handleDragStart = (e: React.DragEvent<HTMLDivElement>, id: number) => {
console.log("drag start");
dragItem.current = e.target as HTMLDivElement;
e.dataTransfer.setData('id', id.toString());
};
const handleDragEnter = (id: number) => {
console.log("entered");
setDraggedOver(id);
};
const handleDragLeave = () => {
console.log("leaved");
setDraggedOver(null);
};
const handleDrop = (e: React.DragEvent<HTMLDivElement>, list: string) => {
console.log("dropped");
e.preventDefault();
const id = Number(e.dataTransfer.getData('id'));
const newCards = [...cards];
const card = newCards.find((c) => c.id === id);
card!.list = list;
setCards(newCards);
setDraggedOver(null);
};
return (
<div className="app">
<div className="lists">
<div className="list todo">
<h2>TODO</h2>
{cards
.filter((c) => c.list === 'TODO')
.map((card) => (
<div
key={card.id}
className="card"
draggable={true}
onDragStart={(e) => handleDragStart(e, card.id)}
onDragEnter={() => handleDragEnter(card.id)}
onDragLeave={handleDragLeave}
onDrop={(e) => handleDrop(e, 'COMPLETED')}
ref={dragItem}
>
{card.title}
</div>
))}
</div>
<div className="list completed">
<h2>COMPLETED</h2>
{cards
.filter((c) => c.list === 'COMPLETED')
.map((card) => (
<div
key={card.id}
className="card"
draggable={true}
onDragStart={(e) => handleDragStart(e, card.id)}
onDragEnter={() => handleDragEnter(card.id)}
onDragLeave={handleDragLeave}
onDrop={(e) => handleDrop(e, 'TODO')}
>
{card.title}
</div>
))}
</div>
</div>
</div>
)
}
export default App;
can anyone help me here? am I missing something?
Note:-I want to do this without using a third-party library.
Seems like this question is already answered here
Why is ondrop not working?
Thanks to azium
You will need to add onDragOver event to those divs as well
I have set of components where it would consist of input fields along with text rows.
As given in the image the users should be able to add categories and description. After adding them they will be rendered as a list of components. like this
Inside a category there will be tags as given in the above image and to add them i have to add a input component. This input component should be available only when the user clicks on the Add tag button below each category row. When a user clicks on it,it should enable the input(should render a input component inside the selected category row) and should be able to type the tag name on it and save it. I need to make this input field enable only when i click on the add tag button. and it should enable only in the selected category row. This is the code that i have tried.
import React, { Component, Fragment } from "react";
import { Button, Header, Input } from "semantic-ui-react";
import "semantic-ui-css/semantic.min.css";
import ReactDOM from "react-dom";
class App extends Component {
state = {
category: "",
description: "",
categories: []
};
onChange = (e, { name, value }) => {
this.setState({ [name]: value });
};
addCategory = () => {
let { category, description } = this.state;
this.setState(prevState => ({
categories: [
...prevState.categories,
{
id: Math.random(),
title: category,
description: description,
tags: []
}
]
}));
};
addTag = id => {
let { tag, categories } = this.state;
let category = categories.find(cat => cat.id === id);
let index = categories.findIndex(cat => cat.id === id);
category.tags = [...category.tags, { name: tag }];
this.setState({
categories: [
...categories.slice(0, index),
category,
...categories.slice(++index)
]
});
};
onKeyDown = e => {
if (e.key === "Enter" && !e.shiftKey) {
console.log(e.target.value);
}
};
tags = tags => {
if (tags && tags.length > 0) {
return tags.map((tag, i) => {
return <Header key={i}>{tag.name}</Header>;
});
}
};
enableTagIn = id => {};
categories = () => {
let { categories } = this.state;
return categories.map(cat => {
return (
<Fragment key={cat.id}>
<Header>
<p>
{cat.title}
<br />
{cat.description}
</p>
</Header>
<Input
name="tag"
onKeyDown={e => {
this.onKeyDown(e);
}}
onChange={this.onChange}
/>
<Button
onClick={e => {
this.addTag(cat.id);
}}
>
Add
</Button>
{this.tags(cat.tags)}
</Fragment>
);
});
};
render() {
return (
<Fragment>
{this.categories()}
<div>
<Input name="category" onChange={this.onChange} />
<Input name="description" onChange={this.onChange} />
<Button onClick={this.addCategory}>Save</Button>
</div>
</Fragment>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
This is the codesandbox url.
Any idea on how to achieve this?.
I changed your code by using function components and react hooks and i created category component which has it own state like this:
import React, { Fragment } from "react";
import { Button, Header, Input } from "semantic-ui-react";
import "semantic-ui-css/semantic.min.css";
import ReactDOM from "react-dom";
const App = () => {
const [Category, setCategory] = React.useState({
title: "",
description: ""
});
const [Categories, setCategories] = React.useState([]);
return (
<div>
{console.log(Categories)}
<Input
value={Category.title}
onChange={e => setCategory({ ...Category, title: e.target.value })}
/>
<Input
value={Category.description}
onChange={e =>
setCategory({ ...Category, description: e.target.value })
}
/>
<Button onClick={() => setCategories([...Categories, Category])}>
Save
</Button>
<div>
{Categories.length > 0
? Categories.map(cat => <CategoryItem cat={cat} />)
: null}
</div>
</div>
);
};
const CategoryItem = ({ cat }) => {
const [value, setvalue] = React.useState("");
const [tag, addtag] = React.useState([]);
const [clicked, setclicked] = React.useState(false);
const add = () => {
setclicked(false);
addtag([...tag, value]);
};
return (
<Fragment>
<Header>
<p>
{cat.title}
<br />
{cat.description}
</p>
</Header>
<Input
name="tag"
value={value}
style={{ display: clicked ? "initial" : "none" }}
onChange={e => setvalue(e.target.value)}
/>
<Button onClick={() => (clicked ? add() : setclicked(true))}>Add</Button>
<div>{tag.length > 0 ? tag.map(tagname => <p>{tagname}</p>) : null}</div>
</Fragment>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
and here a sandbox
I have a navigation menu with three levels of nesting created with a json. When you click on the Parent it shows all the children, regardless of the parent you press.
I would like you to click on the Parent to show only your children and not all the children of all the parents ... I just need to make the distinction between the Parent and the direct Child, which is directly associated with the third level and is shown at the same time
I think that in addition to the conditional this.state.showSubmenu I have to do another one to detect the origin of the Parent, but I am not able to do it. On the other hand I think that one could make a .filter or .reduce to only show the son of the element clicked.
"menu":[
{
"fsttitle":"CRM",
"ico":"auriculares",
"fstmenu":[{
"sndtitle":"Argumentarios",
"ico":"descargar",
"sndmenu":[
{"Id":"114","trdtitle":"SALUD NORMAL1","URL":"https:/argumentarios\/argsalud.pdf","Closable":"1","Autoload":"0","Visible":"1"},
{"Id":"115","trdtitle":"SALUD NORMAL2","URL":"https:/argumentarios\/argsalud.pdf","Closable":"1","Autoload":"0","Visible":"1"},
{"Id":"116","trdtitle":"SALUD NORMAL3","URL":"https:/argumentarios\/argsalud.pdf","Closable":"1","Autoload":"0","Visible":"1"}
]
}]
},
{ "fsttitle":"Permisos",
"ico":"candado",
"fstmenu":[{
"sndtitle":"Herramientas",
"ico":"herramienta",
"sndmenu":[
{"Id":"117","trdtitle":"SALUD NORMAL4","URL":"https:/argumentarios\/argsalud.pdf","Closable":"1","Autoload":"0","Visible":"1"},
{"Id":"118","trdtitle":"SALUD NORMAL5","URL":"https:/argumentarios\/argsalud.pdf","Closable":"1","Autoload":"0","Visible":"1"},
{"Id":"120","trdtitle":"SALUD NORMAL7","URL":"https:/argumentarios\/argsalud.pdf","Closable":"1","Autoload":"0","Visible":"1"}
]
},
{
"sndtitle":"Leads",
"ico":"user-lead",
"sndmenu":[
{"Id":"119","trdtitle":"SALUD NORMAL6","URL":"https:/argumentarios\/argsalud.pdf","Closable":"1","Autoload":"0","Visible":"1"}
]
}]
}]
Edit: I fix my code and now my code works fine. I can show the three levels in my menu.
class Nav extends Component {
constructor(props){
super(props);
this.state = {
navigation: {
menu: [],
},
}
}
componentDidMount() {
fetch('http:json_menuFIN.php')
.then(response => response.json())
.then(data =>{
this.setState({navigation: data});
})
}
render(){
const renderMenu = items => {
return (
<ul className="list">
{items.map((i, key) => {
var icoJson;
if(i.ico){
icoJson = <Icon icon={i.ico} className={"ico-" + i.ico} />;
}
const showHideSubmenu = (index, key) => {
this.setState({
showfstmenu: index,
});
}
var firstMenu = i.fstmenu ? "first-menu" : "";
var secondMenu = i.sndmenu ? "second-menu" : "";
var classMn = i.fstmenu ? "mn-" : "";
var classSb = i.sndmenu ? "sb-" : "";
return (
<li className={`list__item ${firstMenu}${secondMenu}`} key={i.fsttitle + i.sndtitle + i.trdtitle}>
<a
href={i.URL}
className={`${classMn}${classSb}`+i.fsttitle}
onClick={(key) => i.fstmenu ? showHideSubmenu(i.fsttitle, key) : null}>
{icoJson}
<span>{i.fsttitle}{i.sndtitle}{i.trdtitle}</span>
</a>
{i.menu && renderMenu(i.menu)}
{this.state.showfstmenu === i.fsttitle && (
<>{i.fstmenu && renderMenu(i.fstmenu)}</>
)}
<>{i.sndmenu && renderMenu(i.sndmenu)}</>
</li>
)
})}
</ul>
)
}
return (
<nav className="nav">
<div className="menu">
{renderMenu(this.state.navigation.menu)}
</div>
</nav>
)
}
}
You can use key parameter in map function.
Then you can pass index to onClick event handler.
class Nav extends Component {
constructor(props){
super(props);
["showHideSubmenu"].forEach((method) => {
this[method] = this[method].bind(this);
});
this.state = {
navigation: {
menu: [],
},
showSubmenu: -1,
}
}
componentDidMount() {
fetch('http:json_menuFIN.php')
.then(response => response.json())
.then(data =>{
this.setState({navigation: data});
})
}
showHideSubmenu(key){
this.setState({
showSubmenu: key,
});
}
render(){
const renderMenu = items => {
return (
<ul className="list">
{items.map((i, key) => {
var icoJson;
if(i.ico){
icoJson = <Icon icon={i.ico} className={"ico-" + i.ico} />;
}
var firstMenu = i.fstmenu ? "first-menu" : "";
var secondMenu = i.sndmenu ? "second-menu" : "";
var classMn = i.fstmenu ? "mn-" : "";
var classSb = i.sndmenu ? "sb-" : "";
return (
<li className={`list__item ${firstMenu}${secondMenu}`} key={i.fsttitle + i.sndtitle + i.trdtitle}>
<a
href={i.URL}
className={`${classMn}${classSb}`+i.fsttitle}
onClick={(key) => {
if (i.fstmenu) {
this.showHideSubmenu(key);
}
}
>
{icoJson}
<span>{i.fsttitle}{i.sndtitle}{i.trdtitle}</span>
</a>
{i.menu && renderMenu(i.menu)}
{this.state.showSubmenu === key &&
(
<>
{i.fstmenu && renderMenu(i.fstmenu)}
{i.sndmenu && renderMenu(i.sndmenu)}
</>
)
}
</li>
)
})}
</ul>
)
}
return (
<nav className="nav">
<div className="menu">
{renderMenu(this.state.navigation.menu)}
</div>
</nav>
)
}
}
I'm trying to add and remove product when clicking a button, and each button is in different component and the data that I'm getting from is in storeData component where inside there is an object with a true/false status if the status is true the product should display in Cart component if false it will remove the product.
now in ProductList component when I click the add button the status is changing to true, but it's not changing the actual status in storeData component so the result when i go to Cart component nothing is displayed
I know I'm doing this the wrong way, so how can I perform this add and remove operation, i'm new in React.js so please any help would really be appreciated.
ProductList component
import itemlist from "../storeData/storeData";
import { Link } from "react-router-dom";
class ProductList extends Component {
state = {
items: itemlist.items,
addToCart: null
};
addItem(id) {
let itemArray = [];
itemlist.cartItems.filter(target => {
return id === target.id ? itemArray.push(target) : null;
});
const addToCart = itemArray[0];
addToCart.status = false;
this.setState({ addToCart });
}
render() {
return (
<div className="list-wrap">
{this.state.items.map(item => {
return (
<div key={item.id}>
<Link to={{ pathname: "/productdetail", itemdetail: item }}>
<img alt="item img" src={item.posterUrl} />
</Link>
<h2>{item.title}</h2>
<h3>${item.price}</h3>
<button onClick={() => this.addItem(item.id)}>Add to Cart</button>
</div>
);
})}
</div>
);
}
}
export default ProductList;
Cart component
import itemlist from "../storeData/storeData";
class Cart extends Component {
state = {
cart: itemlist.cartItems,
remove: null
};
removeItem(id) {
let itemArray = [];
itemlist.cartItems.filter(target => {
return id === target.id ? itemArray.push(target) : null;
});
let remove = itemArray[0];
remove.status = false;
this.setState({ remove });
}
render() {
return (
<div>
{this.state.cart.map(itm => {
return itm.status === false ? null : (
<div key={itm.id} className="cart-layout">
<img alt="img" src={itm.posterUrl} />
<h4>{itm.title}</h4>
<h4>{itm.price}</h4>
<button onClick={() => this.removeItem(itm.id)}>Remove</button>
</div>
);
})}
</div>
);
}
}
storeData component
let itemlist = {
items: [
{
id: 1,
title: "name 1",
price: "232",
posterUrl:
"https://images-na.ssl-images-amazon.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE#._V1_SX300.jpg"
},
{
id: 2,
title: "name 2",
price: "65",
posterUrl:
"https://images-na.ssl-images-amazon.com/images/M/MV5BMTY5NTc2NjYwOV5BMl5BanBnXkFtZTcwMzk5OTY0MQ##._V1_SX300.jpg"
},
],
cartItems: [
{
id: 1,
status: false,
title: "name 1",
price: "232",
posterUrl:
"https://images-na.ssl-images-amazon.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE#._V1_SX300.jpg"
},
{
id: 2,
status: false,
title: "name 2",
price: "65",
posterUrl:
"https://images-na.ssl-images-amazon.com/images/M/MV5BMTY5NTc2NjYwOV5BMl5BanBnXkFtZTcwMzk5OTY0MQ##._V1_SX300.jpg"
},
]
};
I don't think you are using filter correctly here, in either component. You are confusing the filter test with the action of composing your array. All you need with the filter is a test that will return a boolean and that will construct the array for you.
Try changing:
let itemArray = [];
itemlist.cartItems.filter(target => {
return id === target.id ? itemArray.push(target) : null;
});
To
const itemArray = itemlist.cartItems.filter(target => id === target.id);
And similarly in the cart component.
For more detail on filter see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
could you please tell me how to render a list in react js.
I do like this
https://plnkr.co/edit/X9Ov5roJtTSk9YhqYUdp?p=preview
class First extends React.Component {
constructor (props){
super(props);
}
render() {
const data =[{"name":"test1"},{"name":"test2"}];
const listItems = data.map((d) => <li key={d.name}>{d.name}</li>;
return (
<div>
hello
</div>
);
}
}
You can do it in two ways:
First:
render() {
const data =[{"name":"test1"},{"name":"test2"}];
const listItems = data.map((d) => <li key={d.name}>{d.name}</li>);
return (
<div>
{listItems }
</div>
);
}
Second: Directly write the map function in the return
render() {
const data =[{"name":"test1"},{"name":"test2"}];
return (
<div>
{data.map(function(d, idx){
return (<li key={idx}>{d.name}</li>)
})}
</div>
);
}
https://facebook.github.io/react/docs/jsx-in-depth.html#javascript-expressions
You can pass any JavaScript expression as children, by enclosing it within {}. For example, these expressions are equivalent:
<MyComponent>foo</MyComponent>
<MyComponent>{'foo'}</MyComponent>
This is often useful for rendering a list of JSX expressions of arbitrary length. For example, this renders an HTML list:
function Item(props) {
return <li>{props.message}</li>;
}
function TodoList() {
const todos = ['finish doc', 'submit pr', 'nag dan to review'];
return (
<ul>
{todos.map((message) => <Item key={message} message={message} />)}
</ul>
);
}
class First extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [{name: 'bob'}, {name: 'chris'}],
};
}
render() {
return (
<ul>
{this.state.data.map(d => <li key={d.name}>{d.name}</li>)}
</ul>
);
}
}
ReactDOM.render(
<First />,
document.getElementById('root')
);
<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="root"></div>
Shubham's answer explains very well. This answer is addition to it as per to avoid some pitfalls and refactoring to a more readable syntax
Pitfall : There is common misconception in rendering array of objects especially if there is an update or delete action performed on data. Use case would be like deleting an item from table row. Sometimes when row which is expected to be deleted, does not get deleted and instead other row gets deleted.
To avoid this, use key prop in root element which is looped over in JSX tree of .map(). Also adding React's Fragment will avoid adding another element in between of ul and li when rendered via calling method.
state = {
userData: [
{ id: '1', name: 'Joe', user_type: 'Developer' },
{ id: '2', name: 'Hill', user_type: 'Designer' }
]
};
deleteUser = id => {
// delete operation to remove item
};
renderItems = () => {
const data = this.state.userData;
const mapRows = data.map((item, index) => (
<Fragment key={item.id}>
<li>
{/* Passing unique value to 'key' prop, eases process for virtual DOM to remove specific element and update HTML tree */}
<span>Name : {item.name}</span>
<span>User Type: {item.user_type}</span>
<button onClick={() => this.deleteUser(item.id)}>
Delete User
</button>
</li>
</Fragment>
));
return mapRows;
};
render() {
return <ul>{this.renderItems()}</ul>;
}
Important : Decision to use which value should we pass to key prop also matters as common way is to use index parameter provided by .map().
TLDR; But there's a drawback to it and avoid it as much as possible and use any unique id from data which is being iterated such as item.id. There's a good article on this - https://medium.com/#robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318
Try this below code in app.js file, easy to understand
function List({}) {
var nameList = [
{ id: "01", firstname: "Rahul", lastname: "Gulati" },
{ id: "02", firstname: "Ronak", lastname: "Gupta" },
{ id: "03", firstname: "Vaishali", lastname: "Kohli" },
{ id: "04", firstname: "Peter", lastname: "Sharma" }
];
const itemList = nameList.map((item) => (
<li>
{item.firstname} {item.lastname}
</li>
));
return (
<div>
<ol style={{ listStyleType: "none" }}>{itemList}</ol>
</div>
);
}
export default function App() {
return (
<div className="App">
<List />
</div>
);
}
import React from 'react';
class RentalHome extends React.Component{
constructor(){
super();
this.state = {
rentals:[{
_id: 1,
title: "Nice Shahghouse Biryani",
city: "Hyderabad",
category: "condo",
image: "http://via.placeholder.com/350x250",
numOfRooms: 4,
shared: true,
description: "Very nice apartment in center of the city.",
dailyPrice: 43
},
{
_id: 2,
title: "Modern apartment in center",
city: "Bangalore",
category: "apartment",
image: "http://via.placeholder.com/350x250",
numOfRooms: 1,
shared: false,
description: "Very nice apartment in center of the city.",
dailyPrice: 11
},
{
_id: 3,
title: "Old house in nature",
city: "Patna",
category: "house",
image: "http://via.placeholder.com/350x250",
numOfRooms: 5,
shared: true,
description: "Very nice apartment in center of the city.",
dailyPrice: 23
}]
}
}
render(){
const {rentals} = this.state;
return(
<div className="card-list">
<div className="container">
<h1 className="page-title">Your Home All Around the World</h1>
<div className="row">
{
rentals.map((rental)=>{
return(
<div key={rental._id} className="col-md-3">
<div className="card bwm-card">
<img
className="card-img-top"
src={rental.image}
alt={rental.title} />
<div className="card-body">
<h6 className="card-subtitle mb-0 text-muted">
{rental.shared} {rental.category} {rental.city}
</h6>
<h5 className="card-title big-font">
{rental.title}
</h5>
<p className="card-text">
${rental.dailyPrice} per Night ยท Free Cancelation
</p>
</div>
</div>
</div>
)
})
}
</div>
</div>
</div>
)
}
}
export default RentalHome;
Try this:
class First extends React.Component {
constructor (props){
super(props);
}
render() {
const data =[{"name":"test1"},{"name":"test2"}];
const listItems = data.map((d) => <li key={d.name}>{d.name}</li>;
return (
<div>
{listItems}
</div>
);
}
}