The CSS files are not loading automatically in my React application using Vite

I am facing an issue with importing css and js files into a view in React using vite. The styles are not loading properly, and I have to keep commenting and uncommenting the imports in my code for them to be recognized when entering the view.

Below is a screenshot showing how I import the files in the view:

https://i.sstatic.net/BIPZz.png

Here is my folder tree structure for reference:

https://i.sstatic.net/3VwQx.png

One of the js files named "custom.min.js" is causing this issue:

https://i.sstatic.net/3b97E.png

Answer №1

It's possible that the import path /src/* is incorrect. If the file you are referencing is located within the src folder, then the import path may be wrong. You should use a relative path to reference any .css files.

Regarding the js imports, they need to be named. Since you are using TypeScript and import statements, it is necessary to either name the import or specify what exactly you are importing from the js files. The import statement could look like this:

import fit from "/src/assets/js/*";

or

import { fit } from "/src/assets/js/*";

And in the custom.min.js file, the export should be structured as follows:

export default fit;

or

export { fit };

When it comes to your .css and .js files, assuming your directory structure looks like this:

__src
  |___ assets
  |
  |___ components
  |
  |
  |___ App.tsx
  |___ App.css
  |___ ...(any other files in `src/` from a `create-react-app`)

You will need to import the .css file into the App.tsx like so:

import "./assets/style/style.css"

You can choose to omit the extension if you prefer, although I am not completely certain about doing so for .css files.

I hope this information proves helpful!

Answer №2

If you're attempting to use an absolute path for importing, make sure to eliminate the backslash before the 'src'.

In this scenario, the imports start with '/' indicating that it searches for the 'src' folder within the current directory, 'src/components/organisms/sidebar/src/{your imports}'.

The decision between relative and absolute imports is subjective as both have their pros and cons, this resource will provide clarity, as well as this one.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting to iterate over a JSON object and display its contents using HTML

I have been attempting to iterate through this JSON data and display it in an accordion format similar to the one shown here: but unfortunately, my implementation is not functioning correctly. Here is what I currently have: HTML: <div class="a ...

The table headers in the second table do not match the queryAllSelector

I've encountered an issue with my JavaScript snippet that displays a responsive table. When I use the snippet for a second table with the same class, the formatting gets messed up on mobile devices (try resizing your screen to see). It seems like the ...

Inconsistency in field generation in WordPress Contact Form 7 causing issues

After utilizing the WordPress plugin Contact Form 7 to put together a straightforward questionnaire, I discovered that two fields, Your Birthday and Your Email, were mistakenly generated on top of one another in the final output. You can take a look at th ...

The code compilation of Typescript in a Dockerfile is not functioning as expected due to the error "Name 'process' cannot be found."

Here's the Dockerfile I'm working with: FROM node:latest WORKDIR /usr/src/app ENV NODE_ENV=production COPY package*.json . RUN npm install && npm i -g typescript COPY . . RUN tsc CMD [ "node", "./dist/index.js&qu ...

Issues arise when trying to manage HTML received as a response from a server in plain text

I have a scenario where I am dynamically generating an HTML table on the server side using Java and then sending it to the client as JSON data. The response looks something like this: <table class="table"></table><thead class="thead-dark"&g ...

What is the best way to ensure the image is centered on smaller screens for responsive design?

Greetings from a budding web developer seeking guidance... I've been grappling with positioning the image of a dog inside an iPhone frame at the center when the screen size is reduced (small to medium sizes) to enhance its appearance. Despite my effo ...

Unable to delete React element by ID as it is undefined

Having some trouble deleting an item by ID with React. Despite the backend routes functioning properly (node and postgresql), every attempt to delete an item results in it reappearing upon page refresh. The command line indicates that the item being delete ...

Alert an Angular 2 component about changes triggered by a service

Currently working with Angular 2 and utilizing an AuthService for authentication. I am at a crossroads on how to effectively inform other components when a user logs in or out. Seeking advice on the best approach for managing this situation. Any insights ...

What is the best way to ensure my php variable is easily accessed?

Recently, I've been working on implementing a timer and came across the idea in a post on Stack Overflow. <?php if(($_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_POST['username'])) { //secondsDif ...

What is the simplest way to shift an icon to the right in the header of an Expansion panel using Vuetify

After implementing the template from this example: https://vuetifyjs.com/en/components/expansion-panels/#usage I am facing an issue where the header icon is stuck to the title Even applying "float: right;" does not seem to resolve it Has anyone els ...

What could be causing VueJs2 computed properties to not update?

Check out this updated grid example from the official examples. In my application, I need to reposition the sortOrders array. created: function () { var vm = this; this.columns.forEach(function (key) { vm.sortOrders[key] = 1 }) ...

Adjust the template within a directive to dynamically include an additional directive

Challenge Create a custom directive that dynamically adds the ng-bind attribute, allowing for the use of ng-bind, ng-bind-html, or ng-bind-html-unsafe without needing to manually add it to the template definition throughout. Illustrative Example http://j ...

No data is being recorded in the Firestore database

This component in nextjs is designed to write data to a firestore database after the user clicks a button. Unfortunately, Firebase seems to be having trouble writing the data, even though the alert message following the supposed data dump is successful. I ...

Invoking an *.aspx method from an external HTML file

Greetings, I am a newcomer to the world of web application development. I have successfully created an *aspx page that communicates with a webservice through a method returning a string. My next goal is to invoke this aspx method from a remote HTML 5 page ...

Styling input groups with box shadows in Bootstrap 4

One issue I am encountering is the focus shadow that appears when the input text is focused. My goal is to have both the text input and the right button display a focus border. To illustrate this problem, I have created a JSfiddle: http://jsfiddle.net/a5u ...

Firefox presents a compact box positioned between the images

When attempting to use flags as a way to switch between languages, I encountered an issue in Firefox where a small box appears between the images on the Hebrew site. In Chrome, everything works fine. I implemented the following code: <div class="flags ...

Tips for eliminating the glowing effect when pressing down on the thumb of the material ui slider

Visit Material-UI documentation for Slider component. Despite setting the disabled flag for the entire slider, the thumb continues to show a halo effect when clicked. ...

Oops! Unable to extract the 'devices' property from the unspecified value because it is not defined

I wrote a function in my page.txs file to fetch a JSON response and incorporate it into a Next 13 app page. const getDevices = async () => { try { const res = await fetch('/api/getdevice',{ cache: 'no-store' }) ...

I am looking to incorporate this dynamic option value into my React js state

code image <select className='form-control m-2'> <option value="0">select a doctor</option> {drData.length === 0 ? <option>No Doctor Available</option>: drData.map((option) => ( ...

When attempting to display a sub view in Express, a 404 error is encountered

I am currently working with express and jade to display a web page. My intention is to render the page using this format 'http://127.0.0.1:5000/services/landfreight' but I keep encountering a 404 error. router.get('/service/landfreight' ...