简体   繁体   English

this.props.dispatch没有调用reducer

[英]this.props.dispatch is not calling reducer

Hey guys I have a problem with react-redux. 大家好,我对react-redux有问题。 I have a page Login.js. 我有一个页面Login.js。 When i click a button the changeSite function is called from which i'm trying to dispatch. 当我单击一个按钮时,将尝试从中尝试调度changeSite函数。 I tried a view ways but nothing worked. 我尝试了一种视图方式,但是没有任何效果。 Would be great if anyone can help me. 如果有人可以帮助我,那就太好了。

Login.js: 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. 在定义store的地方,必须将reducer传递给createStore方法。

In the ReduxJS docs you can find an example with a single store and with multiple stores using the combineReducers method. 在ReduxJS文档中,您可以使用combineReducers方法在单个商店多个商店中找到示例

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}; 返回{state}; . .

with this: 有了这个:

. . return state; 返回状态; . .

Thanks for your support 谢谢你的支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM