Simplifying Redux Code with the Duck Duck Pattern in React

Learn how the Duck Duck Pattern can simplify the organization of Redux code in React applications by keeping all code for a feature in one file.

Siddharth Gangwar
BloggingTimes
4 min readMar 6, 2023

--

The Duck Duck Pattern is a design pattern for structuring Redux code in React applications. This pattern aims to simplify the organization of Redux code and reduce the amount of boilerplate code required to set up a Redux store. In this blog post, we will explore the Duck Duck Pattern and how it can be used in React Redux applications.

What is the Duck Duck Pattern?

The Duck Duck Pattern is a Redux code organization pattern that focuses on keeping all of the Redux code for a specific feature or domain in one file, called a "duck". A duck file contains the Redux action types, action creators, and reducers for a specific feature. The name "duck" is a nod to the phrase "if it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck." In other words, if the file contains all of the Redux code for a specific feature, then it is a "duck".

Advantages of the Duck Duck Pattern

The Duck Duck Pattern offers several advantages over other Redux code organization patterns. Some of these advantages include:

1. Simplifies Code Structure

The Duck Duck Pattern simplifies the structure of Redux code by keeping all of the code for a specific feature in one file. This makes it easier to understand the code and reduces the amount of boilerplate code required to set up a Redux store.

2. Encourages Reusability

By keeping all of the code for a specific feature in one file, the Duck Duck Pattern encourages reusability. This makes it easier to reuse code across different parts of the application and reduces the amount of duplication in the code.

3. Easier to Test

The Duck Duck Pattern makes it easier to test Redux code by keeping all of the code for a specific feature in one file. This makes it easier to isolate the code being tested and reduces the amount of setup required to test the code.

Implementing the Duck Duck Pattern

To implement the Duck Duck Pattern, you can create a duck file for each feature or domain in your application. The duck file should contain the action types, action creators, and reducers for that feature. The file should also export the action types, action creators, and reducers so that they can be used by other parts of the application.

Here is an example of a simple duck file:

// ducks/counter.js

// Action Types
const INCREMENT = 'my-app/counter/INCREMENT';
const DECREMENT = 'my-app/counter/DECREMENT';

// Action Creators
export function increment() {
return { type: INCREMENT };
}

export function decrement() {
return { type: DECREMENT };
}

// Reducer
export default function counterReducer(state = 0, action) {
switch (action.type) {
case INCREMENT:
return state + 1;
case DECREMENT:
return state - 1;
default:
return state;
}
}

In this example, we have created a duck file for a simple counter feature. The file contains the action types, action creators, and reducer for the counter feature.

Conclusion

The Duck Duck Pattern is a simple and effective way to organize Redux code in React applications. By keeping all of the code for a specific feature in one file, the pattern simplifies the structure of Redux code and encourages reusability. The pattern also makes it easier to test Redux code by isolating the code being tested. If you are working on a React Redux application, consider using the Duck Duck Pattern to simplify your code and improve its maintainability.

Hey, this blog series is a journey of continual learning and growth as a software engineer: BloggingTimes. By focusing on the core concepts related to software engineering, we will be able to deepen our understanding and improve our skills every day.

Let’s work together to constantly advance our knowledge and expertise in the field of software engineering.

Join me in this exciting journey by clapping, following, and subscribing to this blog.

--

--

Siddharth Gangwar
BloggingTimes

I'm a problem solver at heart. Whether the challenge is big or small, I'm passionate about finding efficient solutions to any type of problem.