Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

javascript - Why my Redux reducer is undefined when I import it

Good evening, I have a small problem. I code an application with React and Redux + Redux Toolkit and simply at the time of importing my reducer in the root reducer, therefore the rootReducer, I realize that my reducer therefore unReducer was not imported correctly, here is the problem.

I put the scope of my rootReducer.js in attachment with a focus on the module in question so FormConnexionReducer which is equivalent to unReducer in my code.

Thank you in advance for your answers.

unReducer.js

//importation des dépendances

const unReducer = createSlice({
  name: 'unReducer',
  initialState: {
    a: '',
    b: '',
  },

  reducers: {
    print_a: () => console.log(a),
    print_b: () => console.log(b)
  },
});

const {print_a, print_b} = unReducer.actions;

export const print_aAction = () =>
  ApplicationStore.dispatch(print_a());

export const print_bAction = () =>
  ApplicationStore.dispatch(print_b());

export default unReducer.reducer;

rootReducer.js


import {combineReducers} from 'redux';
import {default as unReducer} from 'unReducer.js';

export default combineReducers({ // breakpoint, the picture of the scope is at the end of the post
  unReducer,
});

breakpoint scope CLICK ON THE LINK TO SEE THE PICTURE


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your slice file seems to be referencing a store. If the slice references the store file and the store file references the slice file, you have a circular dependency.

JavaScript needs to execute one of the two files first - the imports from the other file will be undefined at that point in time and only filled in later.

Identify your circle and move some stuff out into a third file to break it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

31 comments

40.8k users

...