class LoginPage extends Component {
render() {
return (
<div>
<TextInput/>
<br/>
<TextInput/>
<br/>
<Button onClick={() => {
const dispatcher = useDispatch();
dispatcher(singIn())
}}>SING IN</Button>
</div>
);
}
}
I guess that I am using a hooks in a class component, but what can I use instead of useDispacth
to my LoginPage?
Redux is a library that allows a React app to create a global state object that can be used in any component across the app by consuming the state object.
dispatch is a function of the Redux store. You call store.dispatch to dispatch an action. This is the only way to trigger a state change. With React Redux, your components never access the store directly - connect does it for you. React Redux gives you two ways to let components dispatch actions:
dispatch is a function of the Redux store. You call store.dispatch to dispatch an action. This is the only way to trigger a state change. With React Redux, your components never access the store directly - connect does it for you. React Redux gives you two ways to let components dispatch actions:
With React Redux, your components never access the store directly - connect does it for you. React Redux gives you two ways to let components dispatch actions: By default, a connected component receives props.dispatch and can dispatch actions itself.
dispatch is a function of the Redux store. You call store.dispatch to dispatch an action. This is the only way to trigger a state change. With React Redux, your components never access the store directly - connect does it for you.
For class components, you need to use connect
method from react-redux
. Use connect method and pass mapDispatchToProps
function as shown below.
import { connect } from 'react-redux';
class LoginPage extends Component {
render() {
return (
<div>
<TextInput/>
<br/>
<TextInput/>
<br/>
<Button onClick={() => {
this.props.singIn()
}}>SING IN</Button>
</div>
);
}
}
const mapDispatchToProps = (dispatch) => {
return {
signIn: () => dispatch(signIn())
}
};
export default connect(null, mapDispatchToProps)(LoginPage)
Read thru the doc for more info. https://react-redux.js.org/api/connect
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With