Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Redux toolkit and actions against repetetive code

my code is long, and repetetive. I should use helper function to cut it and make it more maintainable and readable. I am a React beginner and I have a question. Should I do most of this logic with helpers functions in seperate file with actions or inside slice?

component:

const FilterItems: React.FC<FuncProps> = (props) => {
  const dispatch = useDispatch();
  const onChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
    e.preventDefault();
    const content = e.target.value;
    dispatch(
      recipeAction.setFilterType({
        type: props.type,
        set: e.target.checked,
        content: content,
      })
    );
  };

redux slice:
    setFilterType(state, action: PayloadAction<FilterType>) {
          if (action.payload.type === typeOfFiltering.dishType) {
            const chosenType = action.payload.content;
            if (action.payload.set) {
              state.chosenRecipeTypes.push(chosenType);
            }
            if (!action.payload.set) {
              state.chosenRecipeTypes.filter(
                (recipeType) => recipeType !== chosenType
              );
            }
          }
    
          if (action.payload.type === typeOfFiltering.dishLength) {
            const chosenType = action.payload.content;
            if (action.payload.set) {
              state.chosenRecipeLengths.push(chosenType);
            }
            if (!action.payload.set) {
              state.chosenRecipeLengths.filter(
                (recipeType) => recipeType !== chosenType
              );
            }
          }
        },
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you need some functionality in few other components make some helpers functions and reuse it in separate component. If you use it just on one place then you don't need to separate...

also try to use switch instead of if with default case on bottom

and for me looks better if you use destructors instead of this

            const chosenType = action.payload.content;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!