Hey guys I have a problem with react-redux. I have a page Login.js. When i click a button the changeSite function is called from which i'm trying to dispatch. I tried a view ways but nothing worked. Would be great if anyone can help me.
Login.js:
import React from 'react';
import ReactDOM from 'react-dom';
import { Link } from 'react-router';
import { connect } from 'react-redux';
@connect((store) => {
return {
user: store.user.state.user,
registered: store.user.state.registered,
}
})
export default class Login extends React.Component{
handlePassword(e){
}
handleUsername(e){
}
changeSite(){
this.props.dispatch({type: "CHANGE_REG", payload: !this.props.registered});
console.log(this.props.registered);
}
login(){
}
render(){
console.log(this.props.registered);
if(this.props.registered === true){
var loginPage = (
<div>
<h2>Login</h2>
<p>Please login with your credentials</p>
<form>
<input placeholder="Username" type="text"/><br/>
<input placeholder="Password" type="password"/>
<p>You do not have an account yet?</p>
<input type="submit" value="register" onClick={this.changeSite.bind(this)}/>
</form>
</div>
);
}else{
var loginPage = (
<div>
<h2>Register</h2>
<p>Please register with your credentials</p>
<form>
<input placeholder="Username" type="text"/><br/>
<input placeholder="Password" type="password"/><br/>
<input placeholder="repeat Password" type="password"/>
<p>You already have an account?</p>
<input type="submit" value="login"/>
</form>
</div>
);
}
return(
<div>
{loginPage}
</div>
);
}
}
Reducer:
export default function reducer(state={
user:{
id: null,
name: null,
password: null,
},
registered: true,
}, action){
switch(action.type) {
case 'USER_LOGIN':{
return { ...state, user: action.payload}
}
case 'CHANGE_REG':{
return { ...state, registered: action.payload}
}
}
return {state}
}
In the place where you define your store
, you must pass your reducer(s) to the createStore
method.
In the ReduxJS docs you can find an example with a single store and with multiple stores using the combineReducers
method.
Actually it was a very similar solution: I had my state returned as an object and not as the state itself. I changed this:
. . return {state}; . .
with this:
. . return state; . .
Thanks for your support
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.