Skip to content

Latest commit

 

History

History
722 lines (490 loc) · 39.5 KB

redux.md

File metadata and controls

722 lines (490 loc) · 39.5 KB

Redux

What is Redux?
Redux is a JavaScript library for managing application state. It is a predictable state container that helps you write applications that behave consistently across client, server, and native environments, and are easy to test.

Redux is based on three core principles:

  • A single source of truth: The state of your application is stored in a single object tree, which is the only source of truth for the state of your application.
  • Pure reducers: Reducers are pure functions that take the current state and an action object as input and return a new state. This makes them easy to reason about and test.
  • Immutability: The state tree is immutable, meaning that reducers cannot modify the existing state tree. Instead, they create a new state tree based on the current state tree and the action object.

What is Flux?
Flux is a pattern for managing application state. It is similar to Redux, but it is more lightweight and flexible. Flux is based on the following principles:

  • A single source of truth: The state of your application is stored in a single object tree.
  • Unidirectional data flow: All data flows through a single dispatcher. This makes it easy to track changes to the state and debug your application.
  • Pure views: Views are pure functions that take the current state as input and return a UI representation of the state. This makes views easy to test and reason about.

Flux does not have a specific implementation, which makes it flexible to use with different UI frameworks and libraries. However, there are a number of popular Flux libraries available, such as Redux, Alt, and Cycle.js.


Do you need to keep all component states in redux store?
You do not need to keep all component states in the Redux store. In fact, it is not recommended to do so. Redux is best suited for storing global state that needs to be shared between components or persisted across sessions. Component state, on the other hand, is typically ephemeral and only needs to be accessible to the component itself.

There are a few reasons why you should not keep all component states in the Redux store:

  • It can make your code more complex and difficult to maintain.
  • It can lead to performance problems, especially if you have a lot of components.
  • It can make it difficult to test your components in isolation.

Here are some general rules of thumb for deciding what state should be stored in Redux:

  • Store global state. This includes state that is shared between components, such as the user's authentication status, the current page, or the results of a search query.
  • Store state that needs to be persisted. This includes state that needs to be saved to the server or loaded from the server, such as a shopping cart or a user's profile.
  • Store state that is mutated in complex ways. Redux is good at managing complex state transformations, such as undo/redo or real-time updates.

What is Redux DevTools?
Redux DevTools is a browser extension and standalone app that helps you debug your Redux applications. It provides a number of features, including:

  • Time travel: Redux DevTools allows you to step through the history of your state tree, making it easy to track down and fix bugs.
  • Action replay: You can replay individual actions to see how they affect the state tree.
  • State diff: Redux DevTools shows you a diff of the state tree between each action, making it easy to see which parts of the state tree are changing.
  • Customizable UI: You can customize the UI of Redux DevTools to meet your needs.

What are reducers in Redux?
Reducers in Redux are pure functions that take the current state and an action object as input and return a new state. They are responsible for updating the state of the application in response to actions.

Reducers must follow the following rules:

  • They must be pure functions. This means that they must not modify their arguments or have any side effects.
  • They must return a new state object. This means that they cannot mutate the existing state object.
  • They must handle all possible action types. If a reducer does not know how to handle a particular action type, it should return the current state.

Here are some benefits of using reducers in Redux:

  • Predictability: Reducers make it easy to predict how the state of your application will change in response to actions. This is because reducers are pure functions, and they always return the same output for the same input.
  • Debuggability: Reducers make it easy to debug your application, because you can track changes to the state tree over time.
  • Performance: Reducers are very performant, even for large applications.
  • Flexibility: Reducers can be used with any UI framework or library.

What is redux-saga?
Redux-saga is a library that helps you manage side effects in your Redux applications. Side effects are actions that have an impact on the outside world, such as making HTTP requests, updating the DOM, or saving data to the server.

Redux-saga is a powerful tool for managing side effects because it provides a number of features, including:

  • Concurrency: Redux-saga can handle multiple side effects concurrently, without blocking the main thread.
  • Cancellation: Redux-saga can cancel side effects that are in progress. This is useful for preventing unnecessary work from being done, such as when a user navigates away from a page before a request has finished executing.
  • Error handling: Redux-saga provides a robust error handling system that can help you to catch and recover from errors in your side effects.

Redux-saga is implemented using generators, which are a JavaScript feature that allows you to write asynchronous code in a sequential style. This makes it easy to write readable and maintainable code for managing side effects.


How to set initial state in Redux?
There are two main ways to set the initial state in Redux:

  1. Pass the initial state to the createStore() function: When you create a Redux store, you can optionally pass an object as the second argument to specify the initial state. For example:
const store = createStore(reducer, {
  todos: [],
  visibilityFilter: 'all'
});

This code will create a Redux store with the following initial state:

{
  todos: [],
  visibilityFilter: 'all'
}
  1. Use a default value in the reducer: Each reducer can specify a default value for the state that it manages. This is done by using the ES6 default argument value syntax. For example:
function todosReducer(state = [], action) {
  switch (action.type) {
    case 'ADD_TODO':
      return [...state, action.payload];
    default:
      return state;
  }
}

This reducer will return an empty array as the initial state, unless an action is dispatched to update the state.

In general, it is recommended to set the initial state in the reducer, rather than passing it to the createStore() function. This is because it makes it easier to keep your code organized and maintainable.

Here is an example of a reducer that sets its own initial state:

function visibilityFilterReducer(state = 'all', action) {
  switch (action.type) {
    case 'SET_VISIBILITY_FILTER':
      return action.payload;
    default:
      return state;
  }
}

This reducer will return the string "all" as the initial state, unless an action is dispatched to update the state.

Once you have set the initial state in Redux, you can access it anywhere in your application by calling the getState() method on the store. For example:

const state = store.getState();

This code will return the current state of the Redux store.

You can then use the state to update your UI or perform other tasks.


What is the difference between react context and react redux?
React Context and React Redux are both state management solutions for React applications. However, they have different approaches and use cases.

React Context is a built-in API that allows you to share state between components without having to pass props down the component tree. It is a lightweight solution that is well-suited for passing simple data or state between deeply nested components.

React Redux is a third-party library that provides a more robust state management solution. It is a good choice for applications with complex state or that need to share state between many components.

Here is a table that summarizes the key differences between React Context and React Redux:

Feature React Context React Redux
Approach Implicit Explicit
State management Centralized Decentralized
Complexity Lightweight Complex
Use cases Sharing simple data or state between deeply nested components Managing complex state or sharing state between many components

What is redux thunk?
Redux Thunk is a middleware that allows you to dispatch functions instead of plain actions. This can be useful for performing asynchronous operations, such as making HTTP requests or setting delays.

Redux Thunk is a powerful middleware that can be used to perform a variety of tasks, such as making HTTP requests, setting delays, and performing other asynchronous operations.

Here are some benefits of using Redux Thunk:

  • It allows you to perform asynchronous operations in your Redux applications.
  • It is easy to use and implement.
  • It is compatible with other Redux middleware.
  • It is well-maintained and supported by a large community.

If you are using Redux, I recommend using Redux Thunk to perform asynchronous operations in your applications.


How to add multiple middlewares to Redux?
To add multiple middlewares to Redux, you can use the applyMiddleware() function. This function takes an array of middleware as its argument and returns a function that can be used to configure the Redux store's middleware.

For example, the following code adds two middlewares to the Redux store:

import { createStore, applyMiddleware } from 'redux';
import { thunk } from 'redux-thunk';
import { logger } from 'redux-logger';

const store = createStore(reducer, applyMiddleware(thunk, logger));

The applyMiddleware() function will compose the middleware functions and return a function that can be used to dispatch actions. When you dispatch an action, it will be passed through each middleware function in the chain.

You can add as many middlewares as you need to the Redux store. However, it is important to keep in mind that each middleware will add overhead to your application. Therefore, it is important to only add the middlewares that you need.

Here are some tips for adding multiple middlewares to Redux:

  • Use the applyMiddleware() function to compose the middleware functions. This will make it easier to manage your middleware configuration.
  • Add the middleware functions in the order that you want them to be executed. The first middleware in the chain will be executed first.
  • Only add the middlewares that you need. Each middleware will add overhead to your application.
  • Test your application with the middleware enabled to ensure that it is working as expected.

What are the features of redux DevTools?
Redux DevTools is a popular browser extension and standalone app that helps you debug your Redux applications. It provides a number of features, including:

  • Time travel: Redux DevTools allows you to step through the history of your state tree, making it easy to track down and fix bugs.
  • Action replay: You can replay individual actions to see how they affect the state tree.
  • State diff: Redux DevTools shows you a diff of the state tree between each action, making it easy to see which parts of the state tree are changing.
  • Component inspect: You can inspect the props and state of individual components in your application.
  • Customizable UI: You can customize the UI of Redux DevTools to meet your needs.

In addition to these features, Redux DevTools also provides a number of other features that can be helpful for debugging Redux applications, such as:

  • Error logging: Redux DevTools logs errors that occur during the execution of reducers and middleware. This can help you to identify and fix the root cause of errors.
  • Performance profiling: Redux DevTools can be used to profile the performance of your application. This can help you to identify bottlenecks and optimize your code.
  • Integration with other tools: Redux DevTools integrates with other popular debugging tools, such as React Developer Tools and Chrome DevTools. This makes it easy to use Redux DevTools in conjunction with other tools to debug your application.

How to structure redux top level directories?
There is no one-size-fits-all answer to the question of how to structure Redux top-level directories, as the best approach will vary depending on the size and complexity of your application. However, here are some general tips:

  • Group related files together. This could involve grouping reducers, actions, and selectors by feature, or grouping middleware by function.
  • Use descriptive names for your directories and files. This will make it easier to find what you're looking for and understand how your Redux code is organized.
  • Keep your directory structure consistent. This will make your code easier to read and maintain.

Here is an example of a simple Redux directory structure:

src/
  redux/
    actions/
      counter.js
      message.js
    index.js
    reducers/
      counter.js
      message.js
    selectors/
      counterSelector.js
      messageSelector.js
    store/
      index.js

This directory structure groups related files together and uses descriptive names for directories and files. It is also consistent, with each feature having its own directory containing reducers, actions, selectors, and middleware.

For larger and more complex applications, you may want to use a more sophisticated directory structure. For example, you could create a separate directory for each feature, and then subdirectories for reducers, actions, selectors, and middleware. You could also create a directory for global Redux state and middleware.

Here is an example of a more sophisticated Redux directory structure:

src/
  redux/
    features/
      counter/
        actions/
          counter.js
        reducers/
          counter.js
        selectors/
          counterSelector.js
      message/
        actions/
          message.js
        reducers/
          message.js
        selectors/
          messageSelector.js
    globals/
      state.js
      middleware/
        index.js
    index.js

This directory structure groups related files together and uses descriptive names for directories and files. It is also consistent, with each feature having its own directory containing reducers, actions, selectors, and middleware. Additionally, there is a globals directory for global Redux state and middleware.

Ultimately, the best way to structure your Redux top-level directories is to choose a structure that makes sense for your application and that is easy for you and your team to understand and maintain.


What are redux selectors and why to use them?
Redux selectors are functions that take the Redux state as an argument and return a derived value. They are used to encapsulate the logic for deriving data from the state, making it easier to read, maintain, and test your code.

There are a few reasons why you should use Redux selectors:

  • Improved readability: Selectors make your code more readable by encapsulating the logic for deriving data from the state in a single function. This makes it easier to understand how your code works and to track down bugs.
  • Reduced boilerplate: Selectors can help you to reduce boilerplate code by eliminating the need to repeat the same logic for deriving data from the state in multiple places.
  • Improved performance: Selectors can improve the performance of your application by memoizing the results of their calculations. This means that selectors will only recalculate their results if the underlying state has changed.
  • Testability: Selectors are easy to test because they are pure functions. This means that you can easily test the logic for deriving data from the state without having to worry about side effects.

Here is an example of a Redux selector:

const getCounterValue = (state) => state.counter.value;

This selector returns the value of the counter from the state. It is a pure function, meaning that it does not modify the state or produce any side effects.

Here is an example of how to use the selector:

const counterValue = useSelector(getCounterValue);

This code will get the value of the counter from the state and store it in the counterValue variable.


What are the core principles of redux?
The core principles of Redux are:

  • Single source of truth: The state of the entire application is stored in a single object tree, called the store.
  • Pure reducers: Reducers are pure functions that take the current state and an action object as input and return a new state. This makes them easy to reason about and test.
  • Immutability: The state tree is immutable, meaning that reducers cannot modify the existing state tree. Instead, they create a new state tree based on the current state tree and the action object.

What is a store in redux?
A store in Redux is an object that holds the entire state of the application. It is the only source of truth for the state of the application, and all changes to the state must be made through the store.

The store has three main responsibilities:

  1. To hold the current state of the application.
  2. To provide a way to update the state of the application.
  3. To notify listeners when the state of the application changes.

The store provides two methods for updating the state:

  • dispatch(): This method dispatches an action to the store. An action is an object that describes what happened in the application.
  • subscribe(): This method allows listeners to subscribe to changes in the state of the application. When the state changes, the listeners are notified.

The store also provides a method for getting the current state of the application:

  • getState(): This method returns the current state of the application.

What are typical middlewares chooses for handling asynchronous calls in redux?
The most typical middleware choices for handling asynchronous calls in Redux are:

  • Redux Thunk: Redux Thunk is a middleware that allows you to dispatch functions instead of plain actions. This makes it easy to perform asynchronous operations, such as making HTTP requests or setting delays.
  • Redux Saga: Redux Saga is a middleware that provides a more robust and powerful way to handle asynchronous calls. It allows you to define generators that can be used to perform complex asynchronous operations.
  • Redux Observable: Redux Observable is a middleware that uses the RxJS library to manage asynchronous data streams. It is a good choice for applications with complex asynchronous requirements.

Other middleware choices for handling asynchronous calls in Redux include:

  • Redux-Promise: Redux-Promise is a middleware that automatically resolves and rejects promises dispatched to the store.
  • Redux-Loop: Redux-Loop is a middleware that allows you to define state machines to handle asynchronous calls.

The best middleware choice for your application will depend on your specific needs and requirements. If you are unsure which middleware to choose, I recommend starting with Redux Thunk. It is a simple and easy-to-use middleware that is well-suited for most applications.


Are there any similarities between Redux and RxJS?
Yes, there are some similarities between Redux and RxJS. Both libraries are based on the concept of reactive programming, which is a programming paradigm that allows you to write asynchronous code in a declarative and sequential style.

Both Redux and RxJS use the concept of observables, which are streams of data that can be processed over time. Observables can be used to represent a variety of things, such as the results of an HTTP request, the state of a user interface, or the output of a sensor.

Both Redux and RxJS provide operators for transforming and combining observables. This allows you to create complex asynchronous data flows.

However, there are also some key differences between Redux and RxJS. Redux is a state management library, while RxJS is a general-purpose reactive programming library. This means that Redux is specifically designed for managing the state of applications, while RxJS can be used for a variety of tasks, such as data processing, event handling, and user interface programming.

Another key difference is that Redux is a centralized library, while RxJS is a decentralized library. This means that Redux uses a single store to manage the state of the application, while RxJS allows you to manage state in a distributed way.

Finally, Redux is a synchronous library, while RxJS is an asynchronous library. This means that Redux reducers can only modify the state of the application synchronously, while RxJS operators can modify the state of the application asynchronously.


How to access redux store outside react component?
There are two main ways to access the Redux store outside a React component:

  1. Inject the store: You can inject the store into a non-React component by passing it as a prop. For example:
const MyComponent = ({ store }) => {
  // Access the store here
};

const store = createStore(reducer);

<MyComponent store={store} />;
  1. Use a global store: You can create a global store and make it available to all components in your application. This can be done by exporting the store from a module and importing it into the components where you need to access it. For example:
// store.js
export const store = createStore(reducer);

// MyComponent.js
import { store } from './store';

const MyComponent = () => {
  // Access the store here
};

What is Redux form?
Redux Form is a React higher-order component and reducer that helps you manage form state in a Redux store. It provides a number of features, including:

  • Automatic form state management: Redux Form automatically manages the state of your forms, including the values of the form fields, the validity of the form, and the errors on the form fields.
  • Integration with Redux: Redux Form integrates seamlessly with Redux, so you can easily manage your form state alongside the rest of your application state.
  • Validation: Redux Form provides built-in validation for your form fields, so you can easily validate your forms before submitting them.
  • Submission: Redux Form provides a simple way to submit your forms, and it will automatically dispatch actions to the Redux store to update the state of your application.

Here are some of the benefits of using Redux Form:

  • Reduced boilerplate: Redux Form eliminates the need to write a lot of boilerplate code to manage form state.
  • Improved performance: Redux Form is highly performant, even for large and complex forms.
  • Easy to use: Redux Form is easy to use, even for beginners.
  • Well-maintained: Redux Form is a well-maintained library with a large community of users and contributors.

What are the main features of Redux form?
The main features of Redux Form are:

  • Automatic form state management: Redux Form automatically manages the state of your forms, including the values of the form fields, the validity of the form, and the errors on the form fields. This can save you a lot of time and effort, as you don't have to write your own code to manage form state.
  • Integration with Redux: Redux Form integrates seamlessly with Redux, so you can easily manage your form state alongside the rest of your application state. This can make your code more organized and easier to maintain.
  • Validation: Redux Form provides built-in validation for your form fields, so you can easily validate your forms before submitting them. This can help you to catch errors early and prevent users from submitting invalid data.
  • Submission: Redux Form provides a simple way to submit your forms, and it will automatically dispatch actions to the Redux store to update the state of your application. This can make your code more concise and easier to read.

In addition to these main features, Redux Form also provides a number of other features, such as:

  • Field components: Redux Form provides a number of built-in field components that you can use to create your forms. These field components are already connected to Redux Form, so you don't have to do anything to connect them.
  • Asynchronous validation: Redux Form supports asynchronous validation, so you can validate your form fields against a remote server or database.
  • Normalization: Redux Form can automatically normalize your form data before submitting it, which can make your data more consistent and easier to use.
  • Error handling: Redux Form provides robust error handling, so you can catch errors during form validation and submission.
  • Customization: Redux Form is highly customizable, so you can configure it to meet the specific needs of your application.

What are the downsides of redux over flux?
Redux is a more complex and opinionated state management library than Flux. This can be a downside for some developers, who may prefer the flexibility and simplicity of Flux.

Here are some of the downsides of Redux over Flux:

  • Steeper learning curve: Redux has a steeper learning curve than Flux. This is because Redux introduces a number of new concepts, such as reducers, middleware, and immutable state.
  • More boilerplate code: Redux requires more boilerplate code than Flux. This is because Redux has a more complex structure and requires you to write more code to manage your state.
  • Less flexible: Redux is less flexible than Flux. This is because Redux has a number of rules that you must follow, such as the rule that reducers must be pure functions.

Despite these downsides, Redux is a popular choice for state management in React applications. It offers a number of benefits, such as predictability, performance, and scalability.


How to use connect from React Redux?
To use connect from React Redux, you first need to import it:

import { connect } from 'react-redux';

Then, you can use it to connect a React component to the Redux store. To do this, you need to pass the connect function a function that takes the state of the Redux store as input and returns an object of props. This object of props will be passed to the connected React component.

For example, the following code shows how to use connect to connect a simple React component to the Redux store:

import { connect } from 'react-redux';

const MyComponent = ({ counter }) => {
  return (
    <div>
      <h1>Counter: {counter}</h1>
    </div>
  );
};

const mapStateToProps = (state) => {
  return {
    counter: state.counter,
  };
};

const ConnectedMyComponent = connect(mapStateToProps)(MyComponent);

export default ConnectedMyComponent;

In this example, the mapStateToProps function takes the state of the Redux store as input and returns an object of props that contains the value of the counter state slice. This object of props will be passed to the ConnectedMyComponent component.

You can also use connect to dispatch actions to the Redux store. To do this, you need to pass the connect function a function that takes an action as input and dispatches it to the Redux store.

For example, the following code shows how to use connect to dispatch an action that increments the counter:

import { connect } from 'react-redux';
import { incrementCounter } from './actions';

const MyComponent = ({ counter, incrementCounter }) => {
  return (
    <div>
      <h1>Counter: {counter}</h1>
      <button onClick={incrementCounter}>Increment Counter</button>
    </div>
  );
};

const mapStateToProps = (state) => {
  return {
    counter: state.counter,
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    incrementCounter: () => dispatch(incrementCounter()),
  };
};

const ConnectedMyComponent = connect(mapStateToProps, mapDispatchToProps)(MyComponent);

export default ConnectedMyComponent;

In this example, the mapDispatchToProps function takes a dispatch function as input and returns an object that contains a function for incrementing the counter. This function will dispatch an incrementCounter action to the Redux store when it is called.

connect is a powerful tool for connecting React components to the Redux store. It allows you to easily access the state of the Redux store and dispatch actions to the Redux store.


What is the purpose of constants in redux?
Constants in Redux are used to define the types of actions and reducers in your application. This has a number of benefits, including:

  • Readability: Centralizing all of your action types and reducer names in a single file makes your code more readable and easier to understand.
  • Consistency: Using constants ensures that your action types and reducer names are used consistently throughout your code.
  • Error prevention: Using constants helps to prevent errors caused by typos or misspellings in action types and reducer names.
  • Development tools: Some development tools, such as Redux DevTools, can use constants to provide additional features, such as action filtering and grouping.

What are the differences between redux-saga and redux-thunk?
Redux-Saga and Redux-Thunk are both middleware for Redux that allow you to handle asynchronous actions. However, there are some key differences between the two:

Redux-Saga

  • Is a more robust and powerful solution for handling asynchronous actions.
  • Uses generators to control the flow of execution.
  • Allows you to write more readable and maintainable code.
  • Provides features such as cancellation, debouncing, and error handling.

Redux-Thunk

  • Is a simpler and easier-to-use solution for handling asynchronous actions.
  • Uses plain JavaScript functions to control the flow of execution.
  • Provides fewer features than Redux-Saga.

What is a Reducer?
A reducer in Redux is a pure function that takes the current state and an action object as input and returns a new state object. Reducers are responsible for updating the state of the application in response to actions.

Reducers must follow a few simple rules:

  • They must be pure functions. This means that they must not modify the input state object, and they must always return the same output for the same input.
  • They must be deterministic. This means that they must always return the same output for the same input, regardless of the order in which they are called.
  • They must be thread-safe. This means that they can be safely called from multiple threads at the same time.

Reducers are typically written in a functional programming style, which makes them easy to reason about and test.


How to reset state in Redux?
There are two ways to reset the state in Redux:

  1. Dispatch a RESET_STATE action. This is the simplest way to reset the state. To do this, you can dispatch the following action to the Redux store:
dispatch({
  type: 'RESET_STATE',
});

This will cause all reducers to be called with the initial state of the application.

  1. Create a new Redux store. This is a more drastic approach, but it can be necessary in some cases. To do this, you can create a new Redux store with the initial state of the application:
const store = createStore(reducer, initialState);

This will create a new Redux store with the specified initial state.


How to make Ajax request in redux?
To make an Ajax request in Redux, you can use the following steps:

  1. Dispatch an action to the Redux store to indicate that the request is starting. This will allow you to track the loading state of the request and display a loading indicator to the user.
  2. Make the Ajax request using a library such as Axios or Fetch.
  3. Once the request has completed, dispatch an action to the Redux store to update the state with the response data.

Here is an example of how to make an Ajax request in Redux using Axios:

import Axios from 'axios';
import { REQUEST_STARTED, REQUEST_SUCCESSFUL, REQUEST_FAILED } from './actions';

const apiUrl = 'https://example.com/api/products';

export const fetchProducts = async () => {
  dispatch({ type: REQUEST_STARTED });

  try {
    const response = await Axios.get(apiUrl);
    dispatch({ type: REQUEST_SUCCESSFUL, payload: response.data });
  } catch (error) {
    dispatch({ type: REQUEST_FAILED, payload: error });
  }
};

This function first dispatches a REQUEST_STARTED action to the Redux store to indicate that the request is starting. Then, it makes an HTTP GET request to the apiUrl endpoint using Axios. If the request is successful, the function dispatches a REQUEST_SUCCESSFUL action to the Redux store with the response data. If the request fails, the function dispatches a REQUEST_FAILED action to the Redux store with the error.

You can then use the REQUEST_STARTED, REQUEST_SUCCESSFUL, and REQUEST_FAILED actions in your reducers to update the state of the application. For example, you could create a reducer that updates the loading state of the application based on the REQUEST_STARTED and REQUEST_SUCCESSFUL actions. You could also create a reducer that updates the list of products in the state based on the REQUEST_SUCCESSFUL action.

Making Ajax requests in Redux is a simple and straightforward way to manage asynchronous data fetching in your application.


What is the purpose of at(@) symbol in the redux connect decorator?
The @ symbol in the Redux connect() decorator is used to tell Redux that the component is a connected component. Connected components are components that have access to the Redux store and can dispatch actions to it.

The connect() decorator takes two functions as arguments:

  • The first function is a selector function that takes the state of the Redux store as input and returns a subset of the state that the component needs.
  • The second function is a dispatcher function that takes an action object as input and dispatches it to the Redux store.

The @ symbol tells Redux to call the connect() decorator with the selector and dispatcher functions as arguments. The connect() decorator will then return a new component that is connected to the Redux store.


What are the differences between call and put in redux-saga?
call() and put() are two effect creators in Redux Saga. They are used to perform asynchronous and synchronous operations, respectively.

call()

  • Used to call a function and wait for its result.
  • Can be used to call asynchronous functions, such as making HTTP requests or setting delays.
  • Blocks the saga until the function returns.

put()

  • Used to dispatch an action to the Redux store.
  • Can be used to dispatch synchronous or asynchronous actions.
  • Does not block the saga.

Differences

Feature call() put()
Type Asynchronous effect Synchronous effect
Blocking Yes No
Returns Result of the called function Action object

Why are redux state functions called as reducers?
Redux state functions are called reducers because they reduce the state of the application to a new state in response to an action. Reducers are pure functions, which means that they do not modify the input state object, and they always return the same output for the same input.

The name "reducer" comes from the mathematical concept of reduction. Reduction is the process of taking a complex problem and breaking it down into smaller, simpler problems. Redux reducers do the same thing, but with the state of the application.

Reducers are the core of Redux. They are responsible for ensuring that the state of the application is always consistent and predictable.


What is the proper way to access redux store?
The proper way to access the Redux store depends on where you are in your application.

Inside a React component:

The preferred way to access the Redux store inside a React component is to use the connect() function. This function takes a selector function and a dispatcher function as arguments and returns a new component that is connected to the Redux store. The selector function is used to select the state that the component needs from the Redux store, and the dispatcher function is used to dispatch actions to the Redux store.

Outside a React component:

If you need to access the Redux store outside a React component, you can use the store object. The store object is a global object that contains the current state of the Redux store.


What is the mental model of redux-saga?
The mental model of Redux Saga is that it is a separate thread in your application that is solely responsible for side effects. This means that it can handle asynchronous tasks such as data fetching and setting delays without blocking the main thread of your application.

Redux Saga uses generators to control the flow of execution. This allows you to write your asynchronous logic in a more linear and readable way.

Redux Saga also provides a number of features that can help you to write better asynchronous code, such as cancellation, debouncing, and error handling.


How Relay is different from Redux?
Relay and Redux are both state management libraries for JavaScript applications, but they have some key differences.

Relay

  • Is a declarative state management library that uses GraphQL to fetch data.
  • Provides a number of features to make data fetching and caching easier, such as support for fragments and normalized data.
  • Is tightly coupled with GraphQL, so it is best suited for applications that use GraphQL.

Redux

  • Is a functional state management library that uses reducers to update the state of the application.
  • Provides a number of features to make state management easier, such as support for pure functions, immutable data structures, and middleware.
  • Is not coupled to any specific data fetching library, so it can be used with any type of data source.

Here is a table that summarizes the key differences between Relay and Redux:

Feature Relay Redux
Data fetching Declarative, uses GraphQL Functional, uses reducers
Coupling to GraphQL Tightly coupled Not coupled
Other features Fragments, normalized data Pure functions, immutable data structures, middleware