Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mastering Redux and Redux Toolkit

Mastering Redux and Redux Toolkit

Simplified State Management: Redux and Redux Toolkit

Discover the power of Redux and its user-friendly counterpart, Redux Toolkit, in this presentation. We'll explore how Redux simplifies state management and the challenges it addresses. Then, we'll see how Redux Toolkit streamlines Redux with concise code, automatic setup, and best practices. Join us to level up your JavaScript app development!

Cindy Kandie

July 31, 2023
Tweet

More Decks by Cindy Kandie

Other Decks in Programming

Transcript

  1. Introduction To Redux Photo by Pexels Redux is a predictable

    state management library for JavaScript applications. It helps manage the state of an application in a centralized store, making it easier to track changes and maintain a consistent data flow throughout the app. Redux follows a unidirectional data flow pattern and is commonly used with React, though it can be used with other frameworks as well.
  2. useState: for Smaller Apps useState is used to add state

    to functional components. It returns a stateful value and a function to update that value. It allows functional components to have internal state, making them more powerful and capable of handling dynamic data. Photo by Pexels
  3. Photo by Pexels Redux set up in index.js/main.js Root and

    other reducers Store is ideally the state bank CREATE THE STORE Provide the store throughout the application
  4. Photo by Pexels Q: What does React do? A: Passes

    state up then down- which is strenuous State in lower/child components is promote to App.js then passed down Eg BBA to App.js then to A then to AB WHY??!
  5. Q:Why Do You Need Redux? A: To Access State EveryWhere

    Using Store Photo by Pexels Store stores the state and allows any component to access the state. Using the useSelector and <Provider store = {store}>
  6. Introduction to Redux ToolKit: Redux Toolkit is the official opinionated

    package from the Redux team that simplifies the usage of Redux and makes the development experience more efficient. It includes utilities to streamline the creation of Redux stores, reducers, actions, and middleware, reducing boilerplate code and making it easier to set up a Redux-based application. Redux Toolkit also promotes writing code in a more "modern" and concise way, encouraging best practices for working with Redux. Considered as a set of tools to make working with Redux more efficient and developer-friendly. Photo by Pexels
  7. Comparison Between REDUX and REDUX TOOLKIT Photo by Pexels 1.

    Boilerplate Code - Redux: Writing Redux code from scratch often involves creating action types, action creators, and separate reducer functions, leading to A LOT OF boilerplate code. - Redux Toolkit: Redux Toolkit significantly reduces boilerplate code by introducing APIs like `createSlice` and `createAsyncThunk`, which automate the creation of actions and reducers, making the codebase more concise and easier to maintain. 2. Complexity - Redux: Vanilla Redux can be complex, especially for developers new to the library, due to its various concepts like actions, reducers, middleware, and the store. - Redux Toolkit: Redux Toolkit simplifies the structure and usage of Redux by providing a more opinionated approach. It combines actions and reducers into "slices," making it easier to grasp the relationships between different parts of the application.
  8. Comparison Between REDUX and REDUX TOOLKIT Photo by Pexels 3.

    Immutable State Updates - Redux: Ensuring immutability in state updates can be challenging and may require additional libraries or boilerplate code. - Redux Toolkit: Redux Toolkit integrates Immer, which allows developers to write mutable update logic for state while automatically handling the immutability under the hood. 4. Middleware and DevTools Integration - Redux: Configuring middleware and integrating DevTools requires manual setup and additional lines of code. - Redux Toolkit: Redux Toolkit comes with built-in support for common middleware and enables DevTools extension integration by default, simplifying the setup process. 5. Selectors - Redux: Writing selectors to extract specific data from the Redux store can become verbose, especially when dealing with complex state structures. - Redux Toolkit: Redux Toolkit's `createSlice` automatically generates selector functions based on defined reducers, reducing the need for manual selector code and making data retrieval more straightforward.
  9. Comparison Between REDUX and REDUX TOOLKIT Photo by Pexels 6.

    Best Practices - Redux: While Redux provides flexibility, developers may follow different patterns and practices, leading to inconsistency across projects. - Redux Toolkit: Redux Toolkit promotes best practices and a standardized way of writing Redux code, leading to more maintainable and scalable applications. 7. Learning Curve - Redux: Learning and mastering the core concepts of Redux can be time-consuming, particularly for newcomers to the library. - Redux Toolkit: Redux Toolkit abstracts away some of the complexities of Redux, making it more accessible to developers, including those with less experience in managing application state.
  10. SUMMARY Photo by Pexels • Redux Toolkit was built on

    top of Redux to address some of the pain points of working with Redux directly. • It provides a more streamlined and efficient development experience by reducing boilerplate, integrating commonly used tools, simplifying state updates, and promoting best practices. • However, it's worth noting that Redux Toolkit is not a replacement for Redux itself; it is just a set of utilities and conventions that enhance the Redux ecosystem. • Developers can still use traditional Redux if they prefer more control and customization.
  11. Q&A ★ Time for questions ★ Clarifying any doubts or

    queries ★ Interactive discussions- Chip in! Photo by Pexels