Reputation: 420
I want to use a function in my reducer. I need this for sorting. I import this function from another file and all works well. But a question is: Is it valid to import and use functions from somewhere in a reducer?
Upvotes: 0
Views: 1293
Reputation: 412
You can, So basically reducers are pure function's. It is used to handle state mutations in redux. There is no problem of using reducer exported from any file. But the main problem arises when you're not using pure functions which will eventually lead to improper state mutations.
PS : pure functions are those which takes a parameter and returns an output without modifying the original input parameter but by using spreading or destructuring e.t.c
Upvotes: 1
Reputation: 708
Something like you can use in you codebase
import { helperFunc } from '../helper/';
// reducer function
const reducer = () => {
switch(action.type){
case whatever:
const helperfunc = helperFunc();
}
}
Upvotes: 1
Reputation: 1012
Absolutely, as long as your functions are pure functions (so that your reducer remains pure as well). Using sub functions is a great way to keep your reducer readable and maintainable when it doesn't make sense from a state modeling perspective to break things out into a sub-reducer.
Just make sure your sub function doesn't modify the state object but rather that it returns a new sorted object to assign as the next state.
Upvotes: 3