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
Which two programming concepts does Redux Toolkit use internally for createAsyncThunk()?
Middleware and thunks
What is the purpose of middleware in Redux?
Redux middleware extends the store’s abilities and lets you write asynchronous logic that can interact with the store. Middleware is added to the store either through createStore() or configureStore().
How do you register a slice in the store in redux toolkit?
You register a slice in the store by adding its reducer to configureStore under the reducer key.
const store = configureStore({ reducer: { counter: counterSlice.reducer } });
counterSlice is the Slice Object and .reducer is all the reducers in counterSlice.
What does a Reducer do?
A reducer is a function that takes the current state, processes an action, and returns a new state.
How does a component dispatch an Action?
Using dispatch(action), for example:
dispatch(increment()); dispatch(add(5));
What happens after dispatching an Action?
- The action is sent to the store.
- The store forwards it to the appropriate reducer.
- The reducer processes it and returns a new state.
- The UI updates automatically.
How does a component read the current state from the store in toolkit?
const count = useSelector((state) => state.counter.count);
How do you define actions with Redux Toolkit?
Actions are automatically created within createSlice():
const counterSlice = createSlice({ name: "counter", initialState: { count: 0 }, reducers: { increment: (state) => { state.count += 1; }, add: (state, action) => { state.count += action.payload; } } });
You can import and use them like this:
import { increment, add } from "./counterSlice"; dispatch(increment()); dispatch(add(5));