Answer the question
In order to leave comments, you need to log in
How to create components from this JSON in React?
The JSON received from the server is given with the data for the web page. Based on this JSON, it is necessary to implement an adaptive layout of the page in React (in other words, use JSON as a source of data about the page).
Answer the question
In order to leave comments, you need to log in
In short, about the components array from json-a:
1) You make the components themselves, put them in a folder, conditionally, components
2) In components/index.js you write named exports of these components, the name of the exporter must match the type of objects in json -e. Example:
// index.js
export { default as GalleryComponent } from './GalleryComponent';
export { default as GridComponent } from './GridComponent';
import AllComponents from 'components/index'; // index можно не указывать, оставил для наглядности
....
// где-то в методе render:
const componentFromJson; // условно, это твой объект из JSON-a
const CurrentComponent = AllComponents[componentFromJson.type];
return (
<CurrentComponent metadata={ componentFromJson.metadata } /> // пропс metadata только для примера, не забудь прокинуть остальные данные из JSON-a через пропсы
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question