How to pass a react prop if it's deeply nested inside a JSON object?
I'm making a table of products for a shop (the headers are: id, title, imagePath, newPrice, oldPrice) coming from a JSON file and got an ItemTable component created in my React app to iterate over the contents of it.
Here is the code
import React, Component from "react";
import "./ItemTable.css";
class ItemTable extends Component
render()
return (
<table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Image</th>
<th>
Price
<button onClick=() => this.props.priceSort("title")>
Sort
</button>
</th>
<th>Old price</th>
</tr>
</thead>
<tbody>
this.props.data.map(row => (
<tr key=row.id>
<td>row.id</td>
<td>row.data.title</td>
<td>
<img src=row.data.base_url alt="" />
</td>
<td>row.data.price</td>
<td>row.data.oldPrice</td>
</tr>
))
</tbody>
</table>
);
export default ItemTable;
...and here's my App component...
import React, Component from "react";
import ItemTable from "./components/ItemTable/ItemTable";
import shop from "./data/shop.json";
import "./App.css";
class App extends Component
state =
data: shop
;
priceSort = key =>
this.setState(
data: shop.sort((a, b) => parseFloat(b[key]) - parseFloat(a[key]))
);
;
render()
return (
<div className="page-container">
<ItemTable data=this.state.data priceSort=this.priceSort />
</div>
);
export default App;
My priceSort() function dosn't work though because I can't figure out the way to pass a deeply nested JSON prop. Here's a part of my JSON for you to get the picture...
[
"id": 2,
"data":
"price": 990,
"cross_category_id": "733",
"id": "95361595",
"available": "true",
"base_url": "http://image01.bonprix.ru/assets/319x448/1530596198/18164321-KFBpLZ9R.jpg",
"target_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/White_stiletto_high-heels.jpg/300px-White_stiletto_high-heels.jpg",
"title": "High heeled shoes",
"url": "http://ssl.hurra.com/TrackIt?tid=10087150C577PPC&url=[[http://www.bonprix.ru/produkty/pulover-s-vysokim-vorotnikom-chernyj-953615/?landmark=Entry&wkz=98&iwl=291&typ=POR&anbieter=Soloway&aktion=POR_VKB&version=SSP_NAME&promo=promo]]",
"discount": 0,
"categoryId": ["733"],
"description_id": "default",
"star": "4",
"picture": [
"sizetype": "st_w240_h310", "path": "st_w240_h310/2/335/95361595" ,
"path": "st_w300_h500/2/335/95361595", "sizetype": "st_w300_h500" ,
"sizetype": "st_w64_h90", "path": "st_w64_h90/2/335/95361595"
],
"oldPrice": 990
,
"main": 1,
"refs":
The question is: how do i pass the price from my JSON file?
javascript reactjs
|
show 2 more comments
I'm making a table of products for a shop (the headers are: id, title, imagePath, newPrice, oldPrice) coming from a JSON file and got an ItemTable component created in my React app to iterate over the contents of it.
Here is the code
import React, Component from "react";
import "./ItemTable.css";
class ItemTable extends Component
render()
return (
<table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Image</th>
<th>
Price
<button onClick=() => this.props.priceSort("title")>
Sort
</button>
</th>
<th>Old price</th>
</tr>
</thead>
<tbody>
this.props.data.map(row => (
<tr key=row.id>
<td>row.id</td>
<td>row.data.title</td>
<td>
<img src=row.data.base_url alt="" />
</td>
<td>row.data.price</td>
<td>row.data.oldPrice</td>
</tr>
))
</tbody>
</table>
);
export default ItemTable;
...and here's my App component...
import React, Component from "react";
import ItemTable from "./components/ItemTable/ItemTable";
import shop from "./data/shop.json";
import "./App.css";
class App extends Component
state =
data: shop
;
priceSort = key =>
this.setState(
data: shop.sort((a, b) => parseFloat(b[key]) - parseFloat(a[key]))
);
;
render()
return (
<div className="page-container">
<ItemTable data=this.state.data priceSort=this.priceSort />
</div>
);
export default App;
My priceSort() function dosn't work though because I can't figure out the way to pass a deeply nested JSON prop. Here's a part of my JSON for you to get the picture...
[
"id": 2,
"data":
"price": 990,
"cross_category_id": "733",
"id": "95361595",
"available": "true",
"base_url": "http://image01.bonprix.ru/assets/319x448/1530596198/18164321-KFBpLZ9R.jpg",
"target_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/White_stiletto_high-heels.jpg/300px-White_stiletto_high-heels.jpg",
"title": "High heeled shoes",
"url": "http://ssl.hurra.com/TrackIt?tid=10087150C577PPC&url=[[http://www.bonprix.ru/produkty/pulover-s-vysokim-vorotnikom-chernyj-953615/?landmark=Entry&wkz=98&iwl=291&typ=POR&anbieter=Soloway&aktion=POR_VKB&version=SSP_NAME&promo=promo]]",
"discount": 0,
"categoryId": ["733"],
"description_id": "default",
"star": "4",
"picture": [
"sizetype": "st_w240_h310", "path": "st_w240_h310/2/335/95361595" ,
"path": "st_w300_h500/2/335/95361595", "sizetype": "st_w300_h500" ,
"sizetype": "st_w64_h90", "path": "st_w64_h90/2/335/95361595"
],
"oldPrice": 990
,
"main": 1,
"refs":
The question is: how do i pass the price from my JSON file?
javascript reactjs
what does console.log(key) gives you in priceSort ?
– juvian
Nov 13 '18 at 15:35
Hi! It gives me data
– doglabel
Nov 13 '18 at 15:45
try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)
– juvian
Nov 13 '18 at 15:47
Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"
– doglabel
Nov 13 '18 at 15:48
You could use something likedlv
and pass indata.price
as key.shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))
– Tholle
Nov 13 '18 at 15:49
|
show 2 more comments
I'm making a table of products for a shop (the headers are: id, title, imagePath, newPrice, oldPrice) coming from a JSON file and got an ItemTable component created in my React app to iterate over the contents of it.
Here is the code
import React, Component from "react";
import "./ItemTable.css";
class ItemTable extends Component
render()
return (
<table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Image</th>
<th>
Price
<button onClick=() => this.props.priceSort("title")>
Sort
</button>
</th>
<th>Old price</th>
</tr>
</thead>
<tbody>
this.props.data.map(row => (
<tr key=row.id>
<td>row.id</td>
<td>row.data.title</td>
<td>
<img src=row.data.base_url alt="" />
</td>
<td>row.data.price</td>
<td>row.data.oldPrice</td>
</tr>
))
</tbody>
</table>
);
export default ItemTable;
...and here's my App component...
import React, Component from "react";
import ItemTable from "./components/ItemTable/ItemTable";
import shop from "./data/shop.json";
import "./App.css";
class App extends Component
state =
data: shop
;
priceSort = key =>
this.setState(
data: shop.sort((a, b) => parseFloat(b[key]) - parseFloat(a[key]))
);
;
render()
return (
<div className="page-container">
<ItemTable data=this.state.data priceSort=this.priceSort />
</div>
);
export default App;
My priceSort() function dosn't work though because I can't figure out the way to pass a deeply nested JSON prop. Here's a part of my JSON for you to get the picture...
[
"id": 2,
"data":
"price": 990,
"cross_category_id": "733",
"id": "95361595",
"available": "true",
"base_url": "http://image01.bonprix.ru/assets/319x448/1530596198/18164321-KFBpLZ9R.jpg",
"target_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/White_stiletto_high-heels.jpg/300px-White_stiletto_high-heels.jpg",
"title": "High heeled shoes",
"url": "http://ssl.hurra.com/TrackIt?tid=10087150C577PPC&url=[[http://www.bonprix.ru/produkty/pulover-s-vysokim-vorotnikom-chernyj-953615/?landmark=Entry&wkz=98&iwl=291&typ=POR&anbieter=Soloway&aktion=POR_VKB&version=SSP_NAME&promo=promo]]",
"discount": 0,
"categoryId": ["733"],
"description_id": "default",
"star": "4",
"picture": [
"sizetype": "st_w240_h310", "path": "st_w240_h310/2/335/95361595" ,
"path": "st_w300_h500/2/335/95361595", "sizetype": "st_w300_h500" ,
"sizetype": "st_w64_h90", "path": "st_w64_h90/2/335/95361595"
],
"oldPrice": 990
,
"main": 1,
"refs":
The question is: how do i pass the price from my JSON file?
javascript reactjs
I'm making a table of products for a shop (the headers are: id, title, imagePath, newPrice, oldPrice) coming from a JSON file and got an ItemTable component created in my React app to iterate over the contents of it.
Here is the code
import React, Component from "react";
import "./ItemTable.css";
class ItemTable extends Component
render()
return (
<table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Image</th>
<th>
Price
<button onClick=() => this.props.priceSort("title")>
Sort
</button>
</th>
<th>Old price</th>
</tr>
</thead>
<tbody>
this.props.data.map(row => (
<tr key=row.id>
<td>row.id</td>
<td>row.data.title</td>
<td>
<img src=row.data.base_url alt="" />
</td>
<td>row.data.price</td>
<td>row.data.oldPrice</td>
</tr>
))
</tbody>
</table>
);
export default ItemTable;
...and here's my App component...
import React, Component from "react";
import ItemTable from "./components/ItemTable/ItemTable";
import shop from "./data/shop.json";
import "./App.css";
class App extends Component
state =
data: shop
;
priceSort = key =>
this.setState(
data: shop.sort((a, b) => parseFloat(b[key]) - parseFloat(a[key]))
);
;
render()
return (
<div className="page-container">
<ItemTable data=this.state.data priceSort=this.priceSort />
</div>
);
export default App;
My priceSort() function dosn't work though because I can't figure out the way to pass a deeply nested JSON prop. Here's a part of my JSON for you to get the picture...
[
"id": 2,
"data":
"price": 990,
"cross_category_id": "733",
"id": "95361595",
"available": "true",
"base_url": "http://image01.bonprix.ru/assets/319x448/1530596198/18164321-KFBpLZ9R.jpg",
"target_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/White_stiletto_high-heels.jpg/300px-White_stiletto_high-heels.jpg",
"title": "High heeled shoes",
"url": "http://ssl.hurra.com/TrackIt?tid=10087150C577PPC&url=[[http://www.bonprix.ru/produkty/pulover-s-vysokim-vorotnikom-chernyj-953615/?landmark=Entry&wkz=98&iwl=291&typ=POR&anbieter=Soloway&aktion=POR_VKB&version=SSP_NAME&promo=promo]]",
"discount": 0,
"categoryId": ["733"],
"description_id": "default",
"star": "4",
"picture": [
"sizetype": "st_w240_h310", "path": "st_w240_h310/2/335/95361595" ,
"path": "st_w300_h500/2/335/95361595", "sizetype": "st_w300_h500" ,
"sizetype": "st_w64_h90", "path": "st_w64_h90/2/335/95361595"
],
"oldPrice": 990
,
"main": 1,
"refs":
The question is: how do i pass the price from my JSON file?
javascript reactjs
javascript reactjs
edited Nov 13 '18 at 15:26
Federico klez Culloca
16.1k134380
16.1k134380
asked Nov 13 '18 at 15:25
doglabeldoglabel
206
206
what does console.log(key) gives you in priceSort ?
– juvian
Nov 13 '18 at 15:35
Hi! It gives me data
– doglabel
Nov 13 '18 at 15:45
try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)
– juvian
Nov 13 '18 at 15:47
Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"
– doglabel
Nov 13 '18 at 15:48
You could use something likedlv
and pass indata.price
as key.shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))
– Tholle
Nov 13 '18 at 15:49
|
show 2 more comments
what does console.log(key) gives you in priceSort ?
– juvian
Nov 13 '18 at 15:35
Hi! It gives me data
– doglabel
Nov 13 '18 at 15:45
try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)
– juvian
Nov 13 '18 at 15:47
Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"
– doglabel
Nov 13 '18 at 15:48
You could use something likedlv
and pass indata.price
as key.shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))
– Tholle
Nov 13 '18 at 15:49
what does console.log(key) gives you in priceSort ?
– juvian
Nov 13 '18 at 15:35
what does console.log(key) gives you in priceSort ?
– juvian
Nov 13 '18 at 15:35
Hi! It gives me data
– doglabel
Nov 13 '18 at 15:45
Hi! It gives me data
– doglabel
Nov 13 '18 at 15:45
try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)
– juvian
Nov 13 '18 at 15:47
try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)
– juvian
Nov 13 '18 at 15:47
Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"
– doglabel
Nov 13 '18 at 15:48
Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"
– doglabel
Nov 13 '18 at 15:48
You could use something like
dlv
and pass in data.price
as key. shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))
– Tholle
Nov 13 '18 at 15:49
You could use something like
dlv
and pass in data.price
as key. shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))
– Tholle
Nov 13 '18 at 15:49
|
show 2 more comments
1 Answer
1
active
oldest
votes
Because in your priceSort you're no getting any data to sort.
Try this:
priceSort = key =>
this.setState(( data: shop ) =>
const sortedData = shop.sort((a, b) =>
parseFloat(b['data'][key]) - parseFloat(a['data'][key])
);
return data: sortedData
);
;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284243%2fhow-to-pass-a-react-prop-if-its-deeply-nested-inside-a-json-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because in your priceSort you're no getting any data to sort.
Try this:
priceSort = key =>
this.setState(( data: shop ) =>
const sortedData = shop.sort((a, b) =>
parseFloat(b['data'][key]) - parseFloat(a['data'][key])
);
return data: sortedData
);
;
add a comment |
Because in your priceSort you're no getting any data to sort.
Try this:
priceSort = key =>
this.setState(( data: shop ) =>
const sortedData = shop.sort((a, b) =>
parseFloat(b['data'][key]) - parseFloat(a['data'][key])
);
return data: sortedData
);
;
add a comment |
Because in your priceSort you're no getting any data to sort.
Try this:
priceSort = key =>
this.setState(( data: shop ) =>
const sortedData = shop.sort((a, b) =>
parseFloat(b['data'][key]) - parseFloat(a['data'][key])
);
return data: sortedData
);
;
Because in your priceSort you're no getting any data to sort.
Try this:
priceSort = key =>
this.setState(( data: shop ) =>
const sortedData = shop.sort((a, b) =>
parseFloat(b['data'][key]) - parseFloat(a['data'][key])
);
return data: sortedData
);
;
edited Nov 13 '18 at 15:56
Tholle
43.1k54770
43.1k54770
answered Nov 13 '18 at 15:52
Luis GarcíaLuis García
362
362
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284243%2fhow-to-pass-a-react-prop-if-its-deeply-nested-inside-a-json-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what does console.log(key) gives you in priceSort ?
– juvian
Nov 13 '18 at 15:35
Hi! It gives me data
– doglabel
Nov 13 '18 at 15:45
try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)
– juvian
Nov 13 '18 at 15:47
Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"
– doglabel
Nov 13 '18 at 15:48
You could use something like
dlv
and pass indata.price
as key.shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))
– Tholle
Nov 13 '18 at 15:49