I am about to start a project with React-Redux. For the APIs references there are Hooks and connect(). Since, Hooks are the alternate of connect APIs. What is the difference
connect
is a High Order Component
whose job is to provide a way to connect Redux's store to your components
. useSelector
and useDispatch
are the equivalent hooks
. Just another technique to do the same thing.
class Component extends React.Component{
componentDidMount(){
const { fetchApi, arg } = this.props
fetchApi(arg)
}
}
const mapDispatchToProps = dispatch =>({
fetchApi : arg => dispatch(fetchApi(arg))
})
const mapStateToProps = state =>({
arg : state.arg
})
export default connect(mapStateToProps, mapDispatchToProps)(Component)
Now the equivalent using hooks
const Component = () =>{
const dispatch = useDispatch()
const arg = useSelector(state => state.arg)
useEffect(() =>{
dispatch(fetchApi(arg))
},[dispatch, arg])
}
Both do exactly the same thing, it's only a different approach to connect redux
to components
internal state
. Both consume Redux's context to expose dispatch
and state
inside a given component