简体   繁体   中英

How the parameter of the callback function in useSelector hook knows the state of the redux store?

I am studying redux. I came across with useSelector hook that takes a callback function. The parameter of the callback function is the state of the store. My question here is how simply importing useSelector hook from react-redux enables the parameter of the callback function equivalent to the state of the store? Where is the connection between that parameter and the store?

Example

import { useSelector } from 'react-redux'

export const CounterComponent = () => {
  const counter = useSelector((state) => state.counter)
  return <div>{counter}</div>
} 

As you already found out yourself, react-redux relies on the context provider/consumer pattern provided by react itself. react-redux probably assumes that there is a specific context provider somewhere higher up in the component tree. So the connection between the state parameter and the store is made based on a name convention: https://github.com/reduxjs/react-redux/blob/master/src/components/Context.ts#L20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM