React JS Unable to render image - reactjs

I've created a simple React app using npx create-react-app react-portfolio
Created simple page, all things are working properly on my local machine
Pushed to GitHub and published my portfolio into gh-pages (https://yanalinso.github.io/react-portfolio/) and it works!
I changed PCs, and cloned the same code from my repository (https://github.com/yanalinso/react-portfolio)
run npm install and npm start on my local machine, the images does not work anymore (logo and the background picture), but the CSS is working
https://ibb.co/4NchQJG (Result on my local machine)
https://ibb.co/RQVv6KB (Chrome console)
https://ibb.co/ZGN1cPd (File tree, same as with my other machine)
https://ibb.co/qmfwX44 (cmd run result)
My code is simple to use images
img src="img/logo1.png"
I can still push changes on GitHub page and it's working fine,
though images are not working on my local machine.

You need to import it first like this to tell webpack that JS file is using this image
import logo1 from './img/logo1.png';
and then this:
<img src={logo1} />

I tried changing to other picture but still failed,
SOLUTION!
I've restored the package.json to default, so i've removed the config of gh-page on script and removed homepage == IT WORKS!!
Thanks guys for helping :)

I have been searching around regarding this and I find this very strange. I'm a beginner and the solution I can only come back with is
import ANYNAME from "path of file e.g ../images/logo1.png".
import ANYNAME from "../images/logo1.png"
Take note ../ is to reference a file outside the folder you are in.
If in the same folder use ./
Then when you are to use it
<img src={ANYNAME}/>
rather than <img src="../images/logo1.png"/> // This line doesn't work

Related

How to run React App created by create-react-app in Salesforce

I want to run a React app inside salesforce Lightning container. I built the app with npm run build, upload zip file but nothing works. All I can get is a blank page.
Build folder before zip
Zip and uploaded to Static resource
In deveoper console
<aura:application >
<lightning:container src="{!$Resource.Mirage + '/index.html'}"/>
</aura:application>
The preview page is blank and I don't know how to do it properly. I feel like it cannot load my resource.
Do I have to give up on CRA and build custom webpack?
Full disclosure: I never tried this. But it looks like you have to:
To reference a file in an archive, use the URLFOR function.
I am not 100% sure this is the answer.
Let me know.

Fonts doesn`t work in react after using react router

yesterday I wanted to create a landingpage for a subcategory if my website. The site it build with create react app. I created the sub-page using react router. On lokal machine everything works fine.
But after I deployed it on AWS (amplify) the included fonts in the whole app (old site and new one) are not working anymore.
I am getting the "Failed to decode downloaded font: " and "OTS parsing error: invalid version tag" errors.
I didn't changes anything in the index.scss/index.html files.
After I`m running the build command the fonts-files are in the right folder ...
This is how my index.scss looks like:
#font-face {
font-family: "SF Compact Display";
src: url("./assets/fonts/SF-Compact-Display-Regular.otf");
}
#font-face {
font-family: "SF Compact Display Semi";
src: url("./assets/fonts/SF-Compact-Display-Semibold.otf");
}
I also tried it with absolute path :
enter code here src: url("assets/fonts/SF-Compact-Display-Semibold.otf");
But it doesn't work.
I imported the index.scss into my index.tsx import './index.scss'
Both files are living in the src root.
I have read lots of other stories that are similar to my problem. But nothing works.
Does anyone have a guess why I am getting this problem? Is there maybe a redirecting issue with aws as this is the only think which I changed?
(as i said before I included the react router the fonts where working fine ). I also tried to remove the react router and get the old page with the right fonts. But now I have the same issue there.
On aws rewrites I included </^[^.]+$|\.(?!(css|gif|ico|json|jpg|js|png|txt|svg|woff|ttf|otf)$)([^.]+$)/> with /index.html and 200 Rewrite.
Im trying to fix it for hours now, but I don't have any clue what the issue could be.
I hope anyone had the same issue in the past and can help me.
Bests
Not sure if you already solved this, but here're some thoughts to help.
The Failed to decode downloaded font error is almost certainly because the app responded to the browser's request for the font with html. This would likely happen if there's a catch-all route configured for the SPA (e.g. respond with the root index page or a not-found page).
From the info you've shared, the most likely issue is that the relative path to the font files is being broken in the build process. The key insight here is that url("assets/fonts/SF-Compact-Display-Semibold.otf"); isn't actually an absolute path because it's missing the / prefix. If your build's putting the font file in /assets/fonts/SF-Compact-Display-Semibold.otf, what you want is: url("/assets/fonts/SF-Compact-Display-Semibold.otf");.
A useful way to check the directory structure produced by building is to run npm run build (see https://create-react-app.dev/docs/production-build/). This produces a production build in a directory called build. When creating an AWS Amplify project, it automatically sets up running the build process for you (see https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html). By default, for create react app apps, this means it'll do npm run build prior to deploy.

reactjs service worker not found .license file

I'm working on two React projects.
In both projects, the manifest.json file is set up correctly and The serviceworker is also registered.
In one of the projects, the question of adding to the home screen is not asked.
From the research I've done, I've found that a project that doesn't work properly can't find a file in .license format, but in a project that works properly, it's a file with a .license.txt format.
What makes the difference between the two files when it is taken from the building project? How can this problem be solved?
Photo of the project that works properly and displays the install app prompt
The project photo does not work properly and does not show the install app prompt
error message in console
Finally I found the solution to the problem. If you have ejected the project, you can add a field called extractComments with a false value in the webpack.config.js file in the TerserPlugin section. If you have not ejected the project, you can solve this problem by creating a webpack.config.js file and adding the Terser module with the default settings and adding the extractComments field with a false value. The following links may be helpful in understanding and resolving the issue.
license-to-kill-your-PWA
terser-plugin

React serves default content for incorrect paths

I used create-react-app to play with react. I noticed that no matter what I type in after localhost:3000/ (which is the default url on dev machine) I always get the content from localhost:3000
This means I cannot access images so that sort of means I can't really do anything with it.
Is this a bug or is it some awesome new feature that I just don't understand? How can this be switched off?
Create React App uses historyApiFallback for the webpack-dev-server, which means that all requests that give a 404 response will fall back to load the index.html file.
If you e.g. add an image to the public folder, you can see that the image takes precedence over the index.html file.
You can also import the image with the help of Webpack and use the import as src in your code, and Webpack will make sure the image ends up in the final build folder for you.
import img from './image.png';
you can use
render() {
return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;
}
for more information please refer Adding Assets Outside of the Module System

create-react-app markdown-loader for webpack

I've cloned the create-react-app and I would like to use the webpack plugin markdown-loader. Can someone please advise me how I would modify the webpack.config.dev.js to do so.
Thanks
If you don't want to eject out of create-react-app, it's actually fairly simple to do with loader directives.
Install markdown-loader to turn the markdown into HTML
Install html-loader to be able to load HTML into JS
Then:
import YourMarkdown from '!html-loader!markdown-loader!./YOURFILE.md'
export default function MarkdownComponent() {
return <div dangerouslySetInnerHTML={{ __html: YourMarkdown }} />
}
Taken from Dan Abramovs' post here: https://facebook.github.io/react/blog/2016/07/22/create-apps-with-no-configuration.html
All the build settings are preconfigured and can’t be changed.
If you wish to modify any of the settings you can Eject from the app.
“Ejecting” lets you leave the comfort of Create React App setup at any time. You run a single command, and all the build dependencies, configs, and scripts are moved right into your project. At this point you can customize everything you want, but effectively you are forking our configuration and going your own way.
npm run eject will cause all the config options to be moved over to your application giving you full control over the config. - This is a one way process.

Resources