7

I'm learning Redux and Redux Toolkit, but I don't understand why autocomplete doesn't work when I'm trying to dispatch an action (see the image below).

I imported the "action" but WebStorm can't see any methods.

On VSCode it works very well.

enter image description here

enter image description here

Here the action :

import {createSlice} from "@reduxjs/toolkit";

const initialCounterState = { counter: 0, showCounter: true };

const counterSlice = createSlice({
    name: "counter",
    initialState: initialCounterState,
    reducers: {
        increment(state) {
            state.counter++;
        },
        decrement(state) {
            state.counter--;
        },
        increase(state, action) {
            state.counter += action.payload;
        },
        toggleCounter(state) {
            state.showCounter = !state.showCounter;
        },
    },
});

export const counterActions = counterSlice.actions;
export default counterSlice.reducer

Like you can see above , the first image is WebStorm , the second is vscode.

Vscode detects all the methods , WebStorm doesn't , I didn't find any issue like these on google..

I'm wondering if it's simply normal to not see theses methods on WebStorm , it would be weird , WebStorm it's powerful usually..

Yannick
  • 75
  • 6
  • what does the actions definition look like? – lena May 27 '21 at 12:39
  • Maximilian Schwarzmüller ftw! It's odd, I actually had the same exact issue. However, autocomplete worked with me for increment, and ceased to work thereafter. My color scheme for the methods aren't correct either. – Devin B. Jun 01 '21 at 04:18
  • I switched to vscode , I found a lot of little issues like this on webstorm , vscode is better at least for react development, just my opinon. – Yannick Jun 02 '21 at 02:25
  • It's happening the same in my case, using also WebStorm,... and taking the same training course with Maximilian Schwarzmüller at Udemy. The autocompletion is not working for the action generator methods. – Reinier Garcia Sep 03 '21 at 08:30
  • @ReinierGarcia It does not work even in VS Code for me. Any tips? – Paolo Tormon Jan 19 '22 at 05:08

3 Answers3

4

I just found the solution, experimenting with different ways or rearranging my files. I'm taking the same tutorial with the same profesor at Udemy. Is just a matter or organizing your files and imports/exports in a specific way.

Instead of exporting each SliceAction directly from its respective slice file, each one of them must be centralized on the store index file and exported from there.

Solution: Code example:

File: src/store/counterSlice.js

import {createSlice} from '@reduxjs/toolkit';

const counterInitialState = {
    counter: 0,
    showCounter: true,
};

const counterSlice = createSlice({
    name: 'counterSlice',
    initialState: counterInitialState,
    reducers: {
        incrementCounter(state) {
            state.counter++;
        },
        decrementCounter(state) {
            state.counter--;
        },
        increaseByCounter(state, action) {
            state.counter = state.counter + action.payload.amount;
        },
        decreaseByCounter(state, action) {
            state.counter = state.counter - action.payload.amount;
        },
        toggleCounter(state) {
            state.showCounter = !state.showCounter;
        },
    }
});

export default counterSlice;

File: src/store/authSlice.js

import {createSlice} from '@reduxjs/toolkit';

const authInitialState = {
    isAuthenticated: false,
};

const authSlice = createSlice({
    name: 'authSlice',
    initialState: authInitialState,
    reducers: {
        logIn(state) {
            state.isAuthenticated = true;
        },
        logOut(state) {
            state.isAuthenticated = false;
        },
        toggleLogging(state) {
            state.isAuthenticated = !state.isAuthenticated;
        },
    }
});

export default authSlice;

File: src/store/index.js

import {configureStore} from '@reduxjs/toolkit';
import counterSlice from "./counterSlice";
import authSlice from "./authSlice";

const store = configureStore({
    reducer: {
        counterSliceReducer: counterSlice.reducer,
        authSliceReducer: authSlice.reducer,
    },
});

export const counterSliceActions = counterSlice.actions;
export const authSliceActions = authSlice.actions;

export default store;

After organizing your files this way, you will see that now you have a perfect visibility over the action creator methods in your imported CaseReducerActions object (like authSliceActions or counterSliceActions, for example).

So this is how my WebStorm IDE looks like right now:

File: src/App.js

enter image description here

File: src/components/Counter/Counter.jsx

enter image description here

As you can see, now I have auto completion (autocomplete feature) using WebStorm.

Reinier Garcia
  • 1,002
  • 1
  • 11
  • 19
1

In my case, node interpreter for typescript should be provided to intellij ide (webstorm,phpstorm,...) settings

enter image description here

hope it helps someone. cheers.

Nkmelnikov
  • 43
  • 6
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 01 '23 at 09:00
0

Even if you are not using TypeScript directly and only write JavaScript, your editor will still use a library's TypeScript typings to give you things like autocomplete. So even if you are not directly using TypeScript yourself, this is still relevant to you.


This is a known issue, but it is a bit broader:
Unfortunately, WebStorm does not work very well with TypeScript and where other editors just use the library's TypeScript typings (even in JavaScript scenarios) for autocomplete and error messages, WebStorm just randomly guesses wrong stuff because things have kinda similar names.

For Redux Toolkit specifically, there are multiple issues open in their bug tracker and they do a great job at ignoring that: https://youtrack.jetbrains.com/issue/WEB-46527 and https://youtrack.jetbrains.com/issue/WEB-42559 for example.

So yes, due to bugs in WebStorm it is unfortunately "normal to not see theses methods on WebStorm" until the bugs I linked above are fixed.

At this point I can only recommend not using WebStorm for any kind of software development involving JavaScript or TypeScript.
Visual Studio Code might need a few extensions hand-picked and installed to get on feature parity with WebStorm, but it is free and works very well, so I cannot recommend that enough.

E_net4
  • 27,810
  • 13
  • 101
  • 139
phry
  • 35,762
  • 5
  • 67
  • 81
  • 2
    1) In the first paragraph you speak about Webstorm with TypeScript. However, the pertinent code segment shown by Yannick is not in TypeScript. Therefore the 1st paragraph is off topic. 2) I have used Webstorm with TypeScript and it works perfectly well. 3) Webstorm is a professional IDE with a lot more features, right out of the box, than an incomplete free tool as VS Code. The lack of autocomplete in this case is only due to a small lack of organization. Not a big deal. 4) Webstorm could be also free in some cases, as long as the user has a valid email from an educational center. – Reinier Garcia Oct 07 '21 at 09:45
  • That's also not part of what Yannick asked, which makes it again,... being off topic. That's not the solution of what he asked. He didn't asked anything regarding TypeScript. So technically your whole dissertation about TypeScript is off topic in this particular case. He asked something very specific, very clear and concise, very simple. That's all he asked. Cheers. – Reinier Garcia Oct 07 '21 at 14:06