1

I have several pages on my react app.

Edit, Delete, Create, Home

I am using React Router for navigation. I found this issue:

When I refresh a page that has :id in its params like "/games/edit/1" or "/games/details/2" my style.css is not loading. Instead in its it loads "You need to enable JavaScript to run this app." when I inspect the Networking tab in my browser.

NOTE : My style.css is included in the index.html file

Here is my Edit Component :

import { useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { editGame, getGame } from 'services/api/games';
import { getAccessToken } from 'utils/userToken';

export const Edit = () => {
    const navigate = useNavigate();
    const { id } = useParams();

    const [formData, setFormData] = useState({
        category: '',
        title: '',
        maxLevel: '',
        imageUrl: '',
        summary: '',
    });

    useEffect(() => {
        getGame(id).then(({ category, title, maxLevel, imageUrl, summary }) => {
            let newForm = {
                category,
                title,
                maxLevel,
                imageUrl,
                summary,
            };

            setFormData((formData) => newForm);
        });
    }, [id]);

    function onChange(ev) {
        const name = ev.target.name;
        const value = ev.target.value;

        const newForm = {
            ...formData,
            [name]: value,
        };

        setFormData((formData) => newForm);
    }

    async function onSubmite(ev) {
        ev.preventDefault();

        const token = await getAccessToken();
        const response = await editGame(id, token, formData);

        if (response.code === 403) {
            return window.alert('You are not authorized');
        }

        navigate(`/games/${id}`);
    }
    return (
        // <!-- Edit Page ( Only for the creator )-->
        <section id="edit-page" className="auth">
            <form id="edit" onSubmit={onSubmite}>
                <div className="container">
                    <h1>Edit Game</h1>
                    <label htmlFor="leg-title">Legendary title:</label>
                    <input
                        type="text"
                        id="title"
                        name="title"
                        value={formData.title}
                        onChange={onChange}
                    />

                    <label htmlFor="category">Category:</label>
                    <input
                        type="text"
                        id="category"
                        name="category"
                        value={formData.category}
                        onChange={onChange}
                    />

                    <label htmlFor="levels">MaxLevel:</label>
                    <input
                        type="number"
                        id="maxLevel"
                        name="maxLevel"
                        min="1"
                        value={formData.maxLevel}
                        onChange={onChange}
                    />

                    <label htmlFor="game-img">Image:</label>
                    <input
                        type="text"
                        id="imageUrl"
                        name="imageUrl"
                        value={formData.imageUrl}
                        onChange={onChange}
                    />

                    <label htmlFor="summary">Summary:</label>
                    <textarea
                        name="summary"
                        id="summary"
                        onChange={onChange}
                        value={formData.summary}
                    ></textarea>
                    <input
                        className="btn submit"
                        type="submit"
                        value="Edit Game"
                    />
                </div>
            </form>
        </section>
    );
};

1 Answer 1

1

Try importing your CSS into the index.js or App.js file (not sure your folder structure) instead of the in your index.html file.

import '../some/route/style.css';

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah that works. But is it possible to fix this without having to move the css file inside of the src folder ? Because I have public and src folders.
Typically your CSS should be kept with your React components. They are part of the view, one being JSX elements, the other styles. I usually create a small CSS file for every component so that it's easier to maintain. I'm not sure about your folder structure or pattern but typically all your React/CSS belong in your src folder
Huh I may actually transfer my styles to the styled components method as I think its better than including the whole style.css in the index.html file. So thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.