Ad
React - Use JSON To Store Data
Is it a bad idea to use JSON to store data for components? Like this navbar? Is this a bad practice and why?
import json from '../../../dados/links.json';
const NavBar = () => {
return (
<div
className="collapse navbar-collapse justify-content-around"
id="collapsibleNavbar">
<ul className="navbar-nav">
{/* TODO key nao deveria ser index */}
{json.links.map((item, index) => {
return <Link key={index} item={item} />;
})}
</ul>
</div>
);
};
json:
{
"links": [
{
"titulo": "A Associação",
"href": "/sobre"
}
]
};
Ad
Answer
It's a good idea and commonly used for configs:
src |
config |
config1.js
config2.js
index.js
You define a config file as .json
or equivialent javascript Object
:
const config = {
siteTitle: '...',
siteDescription: '...',
siteKeywords: '...',
siteUrl: '...',
siteLanguage: '...',
googleAnalyticsID: '...',
googleVerification: '...',
name: '...',
location: '...',
githubLink: '...'
navLinks: [
{
name: 'About',
url: '#about',
},
{
name: 'Timeline',
url: '#jobs',
},
{
name: 'Projects',
url: '#projects',
},
{
name: 'Contact',
url: '#contact',
},
]
};
export default config;
And use it across components:
import { githubLink, navLinks } from '@config';
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad