1

If I have a value in my appSettings.json file...something like this:

  "AccessControl": {
    "SystemGated": "True"
  },

How can I test that value inside my React component? I am imaging something like this:

export function accessIsGated(value) {
    if (value = "True") {
        return true;
    };
    return false;
}

But I am not sure how I can access that value in my appSettings.json file...

10
  • 1
    Does this answer your question? How to store Configuration file and read it using React Commented Mar 8, 2024 at 13:25
  • @MarioSantini kind of? I am not sure what web pack is though. Commented Mar 8, 2024 at 13:51
  • 3
    is this a remote file? if this is the case, Matteo Di Blasio's answer should hel. Otherwise you can probably just import it import appSettings from '../locales/appSettings.json' and then use it like so: appSettings.AccessControl.SystemGated Commented Mar 8, 2024 at 13:51
  • @Iorweth333 No it is a local file in the same drive as all of my React code. It is part of the project. Commented Mar 8, 2024 at 14:00
  • 1
    I meat what it's going to be like when the application will work in production mode. Or in another words: do you need to change it after you create a production build (i.e. it will be remote) or can it be a part of the bundle (i.e. you need to rebuild and redeploy the project to change it)? Commented Mar 8, 2024 at 14:07

2 Answers 2

2

As i understand you're trying to access an internal json file by importing it in your React component, i'll leave you an example with a simple component example:

import data from "../data.json"
        
        export default function Index() {
          console.log(data)
          
          return (
            <div>
              {data.AccessControl.SystemGated}
            </div>
          );
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Isn't fetch used for contacting an API? Thank you
@SkyeBoniwell i'm sorry for the misunderstanding, try to include the file like this, as iroweth333 suggested
@SkyeBoniwell yes, fetch, among other things, can be used to call an API. But think about what "fetch" means: you are about to fetch a file with your configuration to use it.
1

Below is very simplified example of a React component, which fetches a JSON file and puts it into state for further use:

function MyApp() {
  const [config, setConfig] = React.useState(undefined);
  
  React.useEffect(() => {
    fetch('https:/my.app.com/appSettings.json')
      .then(response => response.json())
      .then(data => setConfig(data));
  }, []);
  
  return (
    <div>
      {config ? JSON.stringify(config) : 'Fetching...'}
    </div>
  );
}

It displays it on the screen, but you can of course do whatever with it, including but not limited to: using it directly, passing it down to children, putting it in a Context to avoid prop-drilling and so on.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.