2

Im developing apps with React and Redux , Router. But now I can't set(update) a state into the state of 'mapStateToProps' function. So a reducer's args 'action' is 'undefined'. I think the cause of this problem is that mapStateToProps's args is not passed from anywhere.

Does anyone know how to pass a state(data input from client) into mapStateToProps function?

My mapStateToProps function is simply written like this.

const mapStateToProps = state=>{
console.log(state);  // this 'state' return undefined.
return {
loginform: state
}};

export default connect(mapStateToProps)(LoginForm)

Here is my code.

Index

/index.js

let store = createStore(showUsername,
window.devToolsExtension && window.devToolsExtension() );

ReactDOM.render(
<Provider store={store}>
<Router history={browserHistory}>
    <Route component={AppNavBar}>
        <IndexRedirect to='/login'>
        <Route path='/login' component={LoginForm} />
    </Route>
</Router>
</Provider>
,
document.getElementById('root')
);

Component

/Loginform.js

class LoginForm extends Component {
    ...
}

const mapStateToProps = state=>{
console.log(state);  // this 'state' return undefined.
return {
    loginform: state
}};


export default connect(mapStateToProps)(LoginForm)

ActionCreator

/actioncreator.js

    export const showusername = 'showUsername';

    export const showUsername =(username,index)=>{
    console.log(username,index);
    return{
        type:showusername,
        username,
        payload:index
    }

    };

#Reducer

/reducer.js

    const initialState = {
            username: '',
            password: '',
            submit: false,
            //request: 'self',
            showselectfield: false
        };

    export default function showUsername(state = initialState, action) {
    switch (action.type) {
      case type.showusername:
      return (
          Object.assign({},initialState ,{username:action.username,password:action.password})
          );

    default:
      return state
  }
}

Thanks,

0

1 Answer 1

0

In your reducer when you check for the action that was triggered, you are checking for type.showusername but since you already have action.type in the switch case you should check for simply the string value i.e. "showusername"

const initialState = {
            username: '',
            password: '',
            submit: false,
            //request: 'self',
            showselectfield: false
        };

    export default function showUsername(state = initialState, action) {
    switch (action.type) {
      case "showusername":
      return (
          Object.assign({},state ,{username:action.username,password:action.password})
          );

    default:
      return state
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! I changed the case value and worked well.But still 'action.username' is undefined.
Are you able to see username in the actionCreator console.log() statement
When I see console.log(username) , it shows undefined.
I also set console.log(state) inside of mapStateToProps function, but the state show undefined too. So I was thinking this is the cause of problem. Is this normal the state of mapStateToProps function show undefined?
showusername takes the username as a input argument, how are you providing that because otherwise it will return null
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.