Paweł Baca
Paweł Baca

Reputation: 888

Imports path in react

Hello I have a lot of imports to reducex e.g.

import {groupAddTeacher} from "../../../../../../redux/actions/group";
import {groupAddEvent} from "../../../../../../redux/actions/group";

How can I replace a path by excluding ../../../../../../

I using webpack

entry: "./src/index.js",

Upvotes: 0

Views: 97

Answers (2)

Jibin Mathews
Jibin Mathews

Reputation: 1135

You can give a alias in your module exports

alias: {
    src: path.resolve(__dirname + '/root')
}

Then you can do something like:

import { groupAddTeacher }from 'root/redux/actions/group'

Upvotes: 1

Avanthika
Avanthika

Reputation: 4182

Use webpack's resolve: https://webpack.js.org/configuration/resolve/

resolve: {
  modules: ['src/redux', 'node_modules'],
  extensions: ['.jsx', '.js'],
  unsafeCache: true,
  alias: {
    actions: path.resolve(__dirname, 'src', 'redux', 'actions')
  }
}

Then you can do

import { groupAddEvent, groupAddTeacher } from 'actions/group';

Hope this solves your problem!

Upvotes: 6

Related Questions