Answer the question
In order to leave comments, you need to log in
How to properly get data from JSON file in React?
Due to not knowing React well enough, I ran into a problem. There is a JSON
file .
There are React components . Link to the entire repository.
At the moment, in the Main.jsx file, I am trying to extract the data from the JSON file "products.json" and carry out further manipulations with them (Product.jsx; Products.jsx).
The end result is "Nothing found" ( Condition in Products.jsx) Question - what is the problem, why can't I display the data? Photo Main.jsx Photo Product.jsx Photo Products.jsx
Answer the question
In order to leave comments, you need to log in
import React from "react";
import { Products } from "../Products";
class Main extends React.Component {
state = {
products: [],
};
componentDidMount() {
fetch("./products.json")
.then((responce) => responce.json())
.then((data) => this.setState({ products: Object.values(data) }));
}
render() {
const { products } = this.state;
return (
<main className="container content">
<Products products={products} />
</main>
);
}
}
export { Main };
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question