0

I have a React project and this is my first time using it. I have a JavaScript in the container that import a JavaScript within the components.

This is my containers file

Restorans.js

import React, { Component } from 'react';
import Restoran from "./components/Restoran/Restoran";
import classes from "./Restorans.module.css";


class Restorans extends Component{
    constructor(props){
        super(props);
        this.state = {
            restorans:
                [
                    {id:1, nama: "Restoran A", alamat: "This is address Restoran A", nomorTelepon : "0217352333"},
                    {id:2, nama: "Restoran B", alamat: "This is address Restoran B", nomorTelepon : "0217352334"},
                    {id:3, nama: "Restoran C", alamat: "This is address Restoran C", nomorTelepon : "0217352335"}
                ],
            isLoading: true


        }
    }

    componentDidMount() {
        console.log("componentDidMount()");
    }

    shouldComponentUpdate(nextProps, nextState) {
        console.log("shouldComponentUpdate()");
    }

    loadingHandler = () => {
        const curretIsLoading = this.state.isLoading;
        this.setState({isLoading: !(curretIsLoading)});
        console.log(this.state.isLoading);
    }

    render() {
        console.log("render()")
        return(
            <React.Fragment>
                <div className={classes.Title}> All Restoran </div>
                <div className={classes.Restorans}>
                    {this.state.restorans.map(restoran =>
                    <Restoran 
                        key={restoran.id}
                        nama={restoran.nama}
                        alamat={restoran.alamat}
                        nomorTelepon = {restoran.nomorTelepon}
                        />
                    )}
                </div>
            </React.Fragment>
        );
    }

}

export default Restorans;

and this is my components

Restoran.js

import React from "react";
import classes from "./Restoran.module.css";

const Restoran = props => {
    return(
        <div className={classes.Restoran}>
            <h3>
                {props.name}
            </h3>
            <p>
                Alamat: {props.alamat}
            </p>
            <p>
                Nomor Telepon: {props.nomorTelepon}
            </p>

        </div>
    )
}

export default Restoran;

My folder library is as follow

enter image description here

When I run npm start on my terminal. I receive this error:

Module not found: Can't resolve './components/Restoran/Restoran' in 'C:\Users\[REDACTED]\projectreact\frontend-projectreact\src\containers\Restorans'

To my own understanding, there shouldn't be any error. I tried to add .js on the end but it still has the same problem. Where did I go wrong?

norbitrial
  • 14,716
  • 7
  • 32
  • 59
Gagak
  • 129
  • 2
  • 13

4 Answers4

1

It looks like you are within the containers directory in Restorans.js.

If you want to access Restoran.js from components, you have to go two levels up and out of containers, so change your import in containers/Restorans.js to:

import Restoran from "./../../components/Restoran/Restoran";

The ../../ part will take you to the root parent directory where both containers and components live as children/sub-directories by moving you up two folders.

As far as CSS, I usually import it one place from in the root <App/> component after a normalizer/CSS Reset.

1

Firstly,(just an advice) try not to name your files same. I see Restorans.module.css both in Restoran and Restorans. It might get clumsy to do so. And next thing is if you want to import css file

import "../Restoran/Restoran.module.css" 

or any specific element then

import {classes} from "../Restoran/Restoran.module.css"

Also , check the path as ,if you are working on Restoran.js then

"../../components/Restoran/Restoran"

.. -> changes to root and then from there you can change your folder and access the required components.

Ayushi Keshri
  • 680
  • 7
  • 18
0

You use incorrect relative path (in Restorans.js), try:

import Restoran from "../../components/Restoran/Restoran";

Also you can set up aliases to have absolute paths (import Restoran from '@components/Restoran/Restoran')

zemil
  • 3,235
  • 2
  • 24
  • 33
0

Defining the path as './../../components/Restoran/Restoran' should help.

kamil-kubicki
  • 598
  • 1
  • 5
  • 13