Two sets of data in one component - reactjs

I'm passing data to a component with no issue - however, I'd now like to re-use that component with a different data set:
import React from 'react';
const people = [{
id: 1,
name: "John",
age: 32,
hobby: "jogging",
},{
id: 2,
name: "John",
age: 32,
hobby: "jogging",
}];
const cars = [{
id: 1,
manufacturer: "Ford",
model: "Focus",
year: "2014",
},{
id: 2,
manufacturer: "Honda",
model: "Civic",
year: "2012",
}];
export default function ListGroup(props) {
return (
<div>
{props.people.map((person) =>
<li key={person.id}>
<p>Name: {person.name}</p>
<p>Hobby: {person.hobby}</p>
</li>
)}
</div>
);
}
The above code works perfectly for the people fixture, but to use the same component I'm not sure what the simplest path might be. Any guidance welcome.

Iterate over the ids using Object.keys(component).map(function(attributes){}. I have presented it like a component you can use it like a function as well. I hope you can convert it to how you want to use it.
class ListGroup extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
{this.props.value.map((component) => {
return (
<li key={component.id}>
{Object.keys(component).map(function(attributes) {
return (
<p>{attributes} : {component[attributes]}</p>
)
}, this)}
</li>
)
})}
</div>
);
}
}
class Hello extends React.Component {
constructor() {
super();
}
render() {
const people = [{
id: 1,
name: "John",
age: 32,
hobby: "jogging",
},{
id: 2,
name: "John",
age: 32,
hobby: "jogging",
}];
const cars = [{
id: 1,
manufacturer: "Ford",
model: "Focus",
year: "2014",
},{
id: 2,
manufacturer: "Honda",
model: "Civic",
year: "2012",
}];
return (
<div>
<ListGroup value={people} />
<ListGroup value={cars} />
</div>
)
}
}
ReactDOM.render(<Hello /> ,document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
<div id="app"></div>

Related

Not being able to render images from an array

I am trying to render product images from and array but they are not being displayed in my browser. There is no error message nor warning. Reactjs app is compyling with no errors.
This is my file with the data:
const sunglasses_card = [
{
id: Math.random(),
product_name: ' Hydra',
price: '173',
currency: '$',
thumb: './images/test.png'
},
{
id: Math.random(),
product_name: 'Oakley Kato',
price: '298',
currency: '$',
thumb: './images/oakley_kato.png'
},
{
id: Math.random(),
product_name: 'Sutro Lite Sweep',
price: '184',
currency: '$',
thumb: './images/sutro_lite.png'
},
{
id: Math.random(),
product_name: 'Encoder',
price: '255',
currency: '$',
thumb: './images/encoder.png'
}
]
export default sunglasses_card;
And this is my dummy component:
import './App.css';
import sunglasses_card from '../src/data/product_data'
function App() {
const sunglases = sunglasses_card
return (
<div className="App">
{sunglases.map((sunglass) => {
return (
<div key={sunglass.id}>
<h1>This is a product</h1>
<p>{sunglass.price}</p>
<img src={sunglass.thumb} alt="anteojos" />
</div>)
})}
</div>
);
}
Price of each product is being rendered perfectly fine. Any reason why am i not being able to display the image?

Parsing error: ',' expected. with array map function

this is very simple react code, but I cannot figure out why it's so mad at me for trying to do a map. I double check the closing brackets and that the key matches and I've tried adding semi colon all over and nothing happened, the error is "Parsing error: ',' expected." on my recipes object but everything checks out. Thanks for any help!
import React from "react";
const RecipeCard = () => {
const recipes = [
{
id: "recipe-id-1",
name: "Tomato Soup",
description: "Love red soup 😎",
ingredient: [
{ name: "tomato", measurement: 3, unit: "cup" },
{ name: "water", measurement: 3, unit: "cup" },
]
},
{
id: "recipe-id-2",
name: "Potato Soup",
description: "Love potato soup 😎",
ingredient: [
{ name: "potato", measurement: 3, unit: "cup" },
{ name: "water", measurement: 3, unit: "cup" },
]
}
];
return (
{recipes.map((recipe) => (
<div className="recipeCard">
<h2>{recipe.name}</h2>
<p>{recipe.description}</p>
<ul>
{recipe.ingredient.map((ingredient) => (
<li>
{ingredient.name} - {ingredient.measurement}
{ingredient.unit}
</li>
))}
</ul>
</div>
))}
)
}
export default RecipeCard;
you need to group the content. use </> fragment
return (
<>
{recipes.map((recipe) => (
<div className="recipeCard">
<h2>{recipe.name}</h2>
<p>{recipe.description}</p>
<ul>
{recipe.ingredient.map((ingredient) => (
<li>
{ingredient.name} - {ingredient.measurement}
{ingredient.unit}
</li>
))}
</ul>
</div>
))}
</>
)
Do not use default export and wrap the output in a div:
import React from "react";
export const RecipeCard = () => {
const recipes = [
{
id: "recipe-id-1",
name: "Tomato Soup",
description: "Love red soup 😎",
ingredient: [
{ name: "tomato", measurement: 3, unit: "cup" },
{ name: "water", measurement: 3, unit: "cup" },
]
},
{
id: "recipe-id-2",
name: "Potato Soup",
description: "Love potato soup 😎",
ingredient: [
{ name: "potato", measurement: 3, unit: "cup" },
{ name: "water", measurement: 3, unit: "cup" },
]
}
];
return (
<div>
{recipes.map((recipe) => (
<div className="recipeCard">
<h2>{recipe.name}</h2>
<p>{recipe.description}</p>
<ul>
{recipe.ingredient.map((ingredient) => (
<li>
{ingredient.name} - {ingredient.measurement}
{ingredient.unit}
</li>
))}
</ul>
</div>
))}
</div>
)
}

line 14 Expected an assignment or function call and instead saw an expression error

I'm am trying to map over seeded data, and I'm getting this error. Does anyone know why? Right now I am only trying to get the key of name to display, but I'm getting an expected an assignment or functionn error.
import { Link } from "react-router-dom";
import {getData} from "../Data"
export default function Home() {
let data = getData();
return (
<div>
{data.map((data) => {
{data.name}
})}
</div>
)
}
This is my seed data that I am trying to pull from.
I have imported it in my home.jsx file and called the function before my return statement.
let data = [
{
name: 'forgive-me-pleases',
image: 'https://i.imgur.com/0UCzcZM.jpg',
price: 50,
tags: ['pink', 'roses', 'bouquet', 'apologies'],
},
{
name: 'pink perfection',
image: 'https://i.imgur.com/XoEClf7.png',
price: 15,
tags: ['pink', 'gerbera daisy', 'singular', 'decor'],
},
{
name: 'hopeful sunshine',
image: 'https://i.imgur.com/LRrcBtN.png',
price: 30,
tags: ['yellow', 'sunflower', 'multiple', 'garden'],
},
{
name: 'motivational mug',
image: 'https://i.imgur.com/NOJ2ikN.png',
price: 25,
tags: ['yellow', 'gerbera daisy', 'singular (with mug)'],
},
{
name: 'breathtaking bouquet',
image: 'https://i.imgur.com/TuuKiHt.png',
price: 40,
tags: ['white', "baby's breath", 'bouquet', 'decor'],
},
{
name: 'loves-me-loves-me-knots',
image: 'https://i.imgur.com/SaTbTEx.png',
price: 20,
tags: ['mixed', 'gerbera daisy', 'mini bouguet', 'for fun'],
},
{
name: 'hiya, spring',
image: 'https://i.imgur.com/dJvHolr.jpg',
price: 35,
tags: ['mixed', 'hyacinths', 'bouquet', 'garden'],
},
{
name: 'can of compassion',
image: 'https://i.imgur.com/PN3jmrf.png',
price: 55,
tags: ['mixed', 'decor', 'bouquet (with can)', 'love'],
},
{
name: 'basket of beauty',
image: 'https://i.imgur.com/Z86X3qq.png',
price: 50,
tags: ['mixed', 'bouquet (with basket)', 'love', 'decor'],
},
];
export function getData() {
return data
}
Please change your code like below
let data = getData();
return (
<div>
{data.map((data) => {
<div>{data.name}</div>
})}
</div>
)

access line information, line id number and onChanged event

How can I get the line information and id number I want to edit.
Also how can I access the onChanged event?
Can you help me ?
I want to get the line information I want to edit.
I want to get the id number of the line I want to edit.
How can I access onChanged events? So how can I understand when textfield changes?
`
import React, { Component } from "react";
import Paper from '#material-ui/core/Paper';
import { EditingState,SearchState,IntegratedFiltering, GroupingState, IntegratedGrouping,} from '#devexpress/dx-react-grid';
import { ColumnChooser, Grid, Table, TableColumnResizing, TableColumnVisibility, TableEditColumn, TableEditRow, TableFixedColumns, TableHeaderRow, Toolbar, SearchPanel, TableGroupRow, GroupingPanel, DragDropProvider,} from '#devexpress/dx-react-grid-material-ui';
class UserInformation extends Component {
constructor(props) {
super(props);
this.state = {
isLoaded: true,
columns: [
{name: 'name', title: 'Name'},
{name: 'city', title: 'city'}
],
rows : [
{ sex: "Female", name: "Sandra", city: "Las Vegas", car: "Audi A4" },
{ sex: "Male", name: "Paul", city: "Paris", car: "Nissan Altima" },
{ sex: "Male", name: "Mark", city: "Paris", car: "Honda Accord" },
{ sex: "Male", name: "Paul", city: "Paris", car: "Nissan Altima" },
{ sex: "Female", name: "Linda", city: "Austin", car: "Toyota Corolla" }
]
};
this.commitChanges = this.commitChanges.bind(this);
//this.getRowId = this.getRowId.bind(this);
}
}
commitChanges({ added, changed, deleted }) {
let { rows } = this.state;
let changedRows;
if (added) {
console.log("added")
}
if (changed) {
changedRows = rows.map(row => (changed[row.id] ? { ...row, ...changed[row.id] } : row));
console.log("changed");
console.log(rows.map(row => (changed[row.id])))
}
if (deleted) {
console.log("deleted")
}
};
//getRowId(row) {
// console.log(this.state.rows.indexOf(row));
// return this.state.rows.indexOf(row);
//}
render() {
const { isLoaded, drList, rows, columns } = this.state;
if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<div className={"animated fadeIn "}>
<Row>
<Paper>
<Grid
rows={rows}
columns={columns}
getRowId={this.getRowId}
>
<EditingState
onCommitChanges={this.commitChanges}
/>
<Table
/*columnExtensions={tableColumnExtensions}*/
/>
<TableHeaderRow />
<TableEditRow />
<TableEditColumn
showAddCommand
showEditCommand
showDeleteCommand
/>
</Grid>
</Paper>
</Row>
</div>
);
}
}
}
export default UserInformation;

How do I iterate through key pair collection?

I have defined some sample data (collection) and trying to iterate through but getting Failed to Compile and it's pointing to render() { in this component:
class RecipeList extends Component {
constructor(props) {
super(props);
this.state = {
recipes: {
"1": {
id: 1,
url: 'http://via.placeholder.com/300x300',
title: '1st Title',
description: 'some decription 1'
},
"1": {
id: 2,
url: 'http://via.placeholder.com/300x300',
title: '1st Title',
description: 'some decription 1'
},
"1": {
id: 3,
url: 'http://via.placeholder.com/300x300',
title: '1st Title',
description: 'some decription 1'
}
}
}
render() {
const recipeIds = Object.keys(this.state.recipes);
const Items = recipeIds.map((recipe) => {
return (
<RecipeListItem
key={recipe}
recipe={recipe}
/>
);
});
return (
<div>
{Items}
</div>
);
}
}
You need unique keys for your recipes. Try the following
class RecipeList extends Component {
constructor(props) {
super(props);
this.state = {
recipes: {
"1": {
id: 1,
url: 'http://via.placeholder.com/300x300',
title: '1st Title',
description: 'some decription 1'
},
"2": {
id: 2,
url: 'http://via.placeholder.com/300x300',
title: '1st Title',
description: 'some decription 1'
},
"3": {
id: 3,
url: 'http://via.placeholder.com/300x300',
title: '1st Title',
description: 'some decription 1'
}
}
}
}
render() {
const recipeIds = Object.keys(this.state.recipes);
const Items = recipeIds.map((key) => {
return (
<div
key={key}>
{this.state.recipes[key].id}
</div>
);
});
return (
<div>
{Items}
</div>
);
}
}
Edit: Updated the code to provide the whole class.

Resources