Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...