Connecting the Search Page
Explore how to connect the search page component to the Redux store in a React application. Understand using useDispatch and useSelector hooks to manage state centrally and dispatch actions. Learn to remove local state by replacing it with Redux store state for cleaner, scalable code and efficient updates.
We'll cover the following...
Steps to connect search page to store
Let’s connect the search page to the store. To do that, perform the following steps:
In
SearchPage.tsx, let’s add the followingimportstatements to import the hooks we need from React Redux and types from our store:
Remove the
QuestionDatatype from the import statement fromQuestionsData.ts: so that it now looks like the following:
Assign a dispatch function to the
useDispatchhook:
Use the
useSelectorhook with a selector to get the searched questions state from the store:
Our local
questionsstate is now redundant, so let’s remove it by removing the highlighted lines:
Inside the
useEffectfunction, remove the reference to the localquestionstate and dispatch the appropriate actions from the store:
In the running app, perform a search operation by entering
typescript, as shown in the following screenshot:
The page will render correctly.
Test yourself
Press the “Run” button to see the output.
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@emotion/react": "^11.10.6",
"@emotion/styled": "^11.10.6",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2",
"@types/node": "^16.18.16",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"history": "^5.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.10.0",
"react-scripts": "5.0.1",
"typescript": "^4.9.5",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"eslint-config-prettier": "^8.7.0",
"eslint-plugin-prettier": "^3.4.1",
"prettier": "^2.8.4"
}
}
The search page is now connected to the store.
That completes connecting the required components to our Redux store.
We accessed the Redux store state by using the useSelector hook from React Redux. We passed a function into this that retrieved the appropriate piece of state we needed in the React component.
To begin the process of a state change, we invoked an action using a function returned from the useDispatch hook from React Redux. We passed the relevant action object into this function, containing information to make the state change.
Notice that we didn’t change the JSX in any of the connected components because we used the same state variable names. We have simply moved this state to the Redux store.