Ad

React - Use JSON To Store Data

- 1 answer

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
Ad