Redux Flashcards
How do you get the current state in redux?
store.getState();
You have three reducers(aReducer, bReducer, cReducer). How do you combine these and create finally a store?
In store.js import all the reducers and createStore and combineReducers like this:
import { createStore, combineReducers } from 'redux'; import { aReducer } from '../features/a/aSlice.js;
Then create a bundled reducers Object:
const reducers = { aSlice: aReducer, etc. ...
Than create the rootReducer
const rootReducer = combineReducers(reducers);
and from there create and export the store:
export const store = createStore(rootReducer);
Which data do you need to pass as props after importing the redux store into your index.js?
The current state of the store and dispatch to the <App></App>:
<App state={store.getState()} dispatch={store.dispatch} />
Where do you import the store in redux?
In the entry point. The index.js:
import { store } from './app/store.js'
How do you get the current state of the store in redux?
store.getState()
How do you listen to changes of the state of the store in redux in your index.js?
store.subscribe(render);
render is the function that renders the whole app component.
How do you dispatch an action in redux?
By calling dispatch() with the action and it’s passed values:
dispatch(action(propA, propB))
How do you import the configureStore from Redux Toolki?
import { configureStore } from '@reduxjs/toolkit'
How do you setup an store in redux toolkit?
To set up a store in Redux Toolkit, you primarily use the configureStore function, which simplifies the process compared to the traditional Redux setup.
export configureStore({ reducer: { todos: todosReducer, filters: filtersReducer } })
What is createSlice in Redux Toolkit?
createSlice is a function that simplifies creating Redux logic by combining reducer, actions, and initialState in one place.
What is the purpose of the name field in createSlice?
- Organizes actions under a common namespace.
- Prevents naming conflicts between slices.
- Improves readability in debugging tools like Redux DevTools.
What are reducers in createSlice?
reducers is an object where each key is an action, and its value is a reducer function that updates the state based on the action.
const counterSlice = createSlice({ name: "counter", initialState: { value: 0 }, reducers: { increment: (state) => { state.value += 1; }, decrement: (state) => { state.value -= 1; } } }); export const { increment, decrement } = counterSlice.actions; export default counterSlice.reducer;
Write a sample createSlice configuration for a counter with actions to increment and decrement.
const counterSlice = createSlice({ name: 'counter', initialState: { value: 0 }, reducers: { increment: (state) => { state.value += 1; }, decrement: (state) => { state.value -= 1; } } });
How can you make actions generated by createSlice available?
You make actions available by exporting them from the slice file:
export const { increment, decrement } = counterSlice.actions;
This allows them to be imported and used in components:
import { increment } from './counterSlice';
Then, you can dispatch them when needed:
dispatch(increment());
In Redux/toolkit createAsyncThunk() accepts two arguments. In order, they are?
createAsyncThunk‘s arguments are an action type string and an asynchronous thunk function.
What is a thunk in redux?
A function returned by another function