0

I'm a beginner on Reactjs ans Javascript.
I read that create-react-app can access the public folder like this to get an image:

  img src={process.env.PUBLIC_URL + '/img/logo.png'} />;

But this also works but cant find documentation about it:

  let photo = '/img/logo.png';

and another thing is if I try to read a json file the same way it fails:

let someJson = process.env.PUBLIC_URL + 'resume.json';

test code

 return (
      <div className="App">
      <header className="App-header">
        <img src={photo} className="App-logo" alt="logo" />
        <p>
         {someJson.basics.name} // FAIL ...
        </p>
      </header>
    </div>
  );

json

{
  "basics": {
    "name": "Foo Bar"
    }
}

Why can I get the image and not the json?

Nicolas Hevia
  • 15,143
  • 4
  • 22
  • 31
Kid
  • 1,869
  • 3
  • 19
  • 48
  • Hey! Can you share the resume.json file –  Aug 25 '20 at 10:24
  • Any specific reason you want to put it in Public Folder –  Aug 25 '20 at 10:30
  • The json is there in my question!. All the app props come from a json file and I want to move it to public folder so I can change it and not have to rebuild the app. I learn reatcjs and dont even know if this is possible – Kid Aug 25 '20 at 11:45

1 Answers1

0

You can import your JSON file inside the component. This is the recommended way, instead of using the public var path which has its downsides.

From the React docs

You can also add other assets to the public folder. Note that we normally encourage you to import assets in JavaScript files instead (...) If you put a file into the public folder, it will not be processed by webpack. Instead it will be copied into the build folder untouched. To reference assets in the public folder, you need to use an environment variable called PUBLIC_URL.

import React from "react";
// resume JSON file is at the same level than this file
import resume from "./resume.json";

export default function App() {
  return (
    <div className="App">
      <p>{resume.basics.name}</p>
    </div>
  );
}

Listed downsides from using %PUBLIC_URL% (in Node) or process.env.PUBLIC_URL (in JavaScript)

  • None of the files in public folder get post-processed or minified.
  • Missing files will not be called at compilation time, and will cause 404 errors for your users.
  • Result filenames won’t include content hashes so you’ll need to add query arguments or rename them every time they change.
Nicolas Hevia
  • 15,143
  • 4
  • 22
  • 31
  • Thanks this is what I use now `import resume fr..` I have a [creat-react-app](http://greta.portplays.com/) that get all it's props from a json file. I want to in the future have a backend and for now I want to control the app content from the json file in the public folder – Kid Aug 25 '20 at 11:26
  • why cant I read the json but the image from the public folder? – Kid Aug 25 '20 at 11:32
  • I learn reatcjs and dont even know if this is possible – All the app props come from a json file and I want to move it to public folder so I can change it and not have to rebuild the app. – Kid Aug 25 '20 at 11:47