Banging my head against a wall here while making the UI for my app using React/Redux. The problem is that when my action is fired-off by the client mutating state it turns the piece of state I'm changing into a nested object.
In this case I am trying to change the piece of my state loginForm to either be "login" or "signUp". When I fire off my action the prop loginForm (passed down into my component from state) looks like this: this.props.loginForm.loginForm = "login".
client-side code:
import React from "react";
import LoginForm from "../minor/LoginForm";
const Login = React.createClass({
displayForm(){
if (this.props.loginForm === "login"){
return <span>Login</span>
} else if (this.props.loginForm === "signUp"){
return <span>Sign Up</span>
} else {
console.log("meh")
}
},
renderLogin(){
this.props.chooseForm("login");
},
renderSignUp(){
this.props.chooseForm("signUp");
},
render(){
return(
<div className="loginBox">
<h1>Slots</h1>
<button onClick={this.renderLogin} name="login" >Login</button>
<button onClick={this.renderSignUp} name="signUp" >Sign Up</button>
{this.displayForm()}
</div>
)
}
});
export default Login;
userActions.js
export function chooseForm(form){
console.log("choosing form")
return {
type: "CHOOSE_FORM",
form
}
}
userReducer.js
export function loginForm(state=null, action){
switch(action.type){
case "CHOOSE_FORM":
return {...state, loginForm: action.form };
default:
return state;
}
}
The aim here is to then display the proper component associated with the value of this.prop.loginForm which is unachievable due to my issue stated above.
Update
mainReducer.js (combinedReducer)
import {combineReducers} from "redux";
import {routerReducer} from "react-router-redux";
// reducers
import { loginStatus, loginForm }from "./userReducer";
const rootReducer = combineReducers({ loginStatus, loginForm, routing: routerReducer});
export default rootReducer;