React Redux操作未调度但请求成功

时间:2018-05-14 04:40:31

标签: javascript reactjs redux fetch

我试图在我的项目上实现身份验证,因为标题说它注册了一个用户,但没有调度操作。我有几乎相同的提取数据的动作,它的工作原理,调度动作。是功能:

export const signIn = data => dispatch => {
  dispatch({ 
    type: SIGN_UP 
    })
  fetch(API_URL+'/register', { 
   method: 'POST',
   headers: {
      'content-type': 'application/json'
      },
   body: JSON.stringify(data),
    })
      .then(response => response.json())
      .then(message => dispatch({
         type: SIGN_UP_SUCCESS, payload: message
         }))
      .catch(error => dispatch({
        type: SIGN_UP_FAILED, payload: error
      }))
}

减速器:

export const authReducer = (state = initialState, action) => {
  switch(action.type) {
    case SIGN_UP: 
      return {
        ...state,
        loading: true
      }
    case SIGN_UP_SUCCESS: 
      return {
        ...state,
        loading: false,
        message: action.payload
      }
    case SIGN_UP_FAILED:
      return {
        ...state,
        loading: false,
        error: action.payload
      }
    default: 
      return state

  }
}

连接方法:

export default connect(null, { signIn })(RegisterForm);

注册表单组件代码(只是为了满足Stackoverflow的愿望):



import React, { Component } from 'react';
import { connect } from 'react-redux';

import { Form, Button, Message, Field } from 'semantic-ui-react';

import validator from 'email-validator';

import { signUp } from '../../actions/authActions';


class RegisterForm extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      data: {
        username: '',
        name: '',
        email: '',
        password: '',
        city: '',
        address: ''
      },
      errors: {}
    }
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  

  handleChange = e => {
    this.setState({
        ...this.state,
        data: { ...this.state.data, [e.target.name]: e.target.value}
    })
  }

  handleSubmit = e => {
    console.log(this.state.data)
    e.preventDefault();
    const errs = this.validate(this.state.data);
    this.setState({
      errors: errs
    });
    if(Object.keys(this.state.errors).length === 0) {
      this.props.signUp(this.state.data)
    }
  }

  validate = data => {
    const errors = {};

    if(!data.username) errors.username = 'Username is required';
    if(!data.name) errors.name = 'Name is required';
    if(!data.email) errors.email = 'Email is required';
    if (!validator.validate(data.email)) errors.email = "Invalid email";
    if(!data.password) errors.password = 'Password is required';
    if(!data.city) errors.city = 'City is required';
    if(!data.address) errors.address = 'Address is required'

    return errors
   }

render() {
  const { errors, data } = this.state
     return <Form onSubmit={this.handleSubmit}>
                <Form.Field>
                <label>Username</label>
                <input 
                placeholder='Username'
                name="username"
                type="text"
                onChange={this.handleChange}
                value={this.state.data.username}
                 />
                 </Form.Field>
                 {errors.username && <Message error header={errors.username}/>}
             
                <Form.Field>
                <label>Name</label>
                <input 
                placeholder='Name'
                name="name"
                type="text"
                onChange={this.handleChange}
                value={this.state.data.name} 
                 />
                 </Form.Field>
                 {errors.name && <Message error header={errors.name}/>}
             
                <Form.Field>
                <label>Address</label>
                <input 
                placeholder='Address'
                name="address"
                type="text"
                onChange={this.handleChange}
                value={this.state.data.address} 
                 />
                 </Form.Field>
                 {errors.address && <Message error header={errors.address}/>}


                <Form.Field>
                <label>City</label>
                <input 
                placeholder='City'
                name="city"
                type="text"
                onChange={this.handleChange}
                value={this.state.data.city} 
                 />
                 </Form.Field>
                 {errors.city && <Message error header={errors.city}/>}

                 <Form.Field>
                <label>Email</label>
                <input 
                placeholder='Email'
                name="email"
                type="email"
                onChange={this.handleChange}
                value={this.state.data.email}
                 />
                 </Form.Field>
                 {errors.email && <Message error header={errors.email}/>}

                  <Form.Field>
                <label>Password</label>
                <input 
                placeholder='Password'
                name="password"
                type="password"
                onChange={this.handleChange}
                value={this.state.data.password} 
                 />
                 </Form.Field>
                 {errors.password && <Message error header={errors.password}/>}

              <Button type='submit'>Register</Button>
        </Form>
  }
}

export default connect(null, { signUp })(RegisterForm);
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您的代码似乎没问题,请确保您的redux-devtools已正确实施。

const store = createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(), applyMiddleware(thunk)) // [, rest of middlewares]

答案 1 :(得分:0)

您是否在组件中使用了bindActionCreators?在handleSubmit中你刚刚调用了动作而没有发送它