简体   繁体   English

我的反应应用程序中的消息如何解决这个问题

[英]message in my react app how to solve this

It keeps showing me this message in react, redux app I have tried to fix it but nothing work and actually I can't know what is the problem in my code它一直在反应中向我显示此消息,redux 应用程序我已尝试修复它但没有任何效果,实际上我不知道我的代码中有什么问题

Unhandled Rejection (TypeError): props.setAlerts is not a function未处理的拒绝(TypeError):props.setAlerts 不是函数

This is my store这是我的商店

import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

const initialState = {};

const middleware = [thunk];

const store = createStore(
  rootReducer,
  initialState,
  composeWithDevTools(applyMiddleware(...middleware))
);

export default store;

This is my function这是我的功能

import { SET_ALERT, REMOVE_ALERT } from './types';
import { v4 as uuidv4 } from 'uuid';

export const setAlerts = (masg, alertType) => dispatch => {
  const id = uuidv4();
  dispatch({
    type: SET_ALERT,
    payload: { masg, alertType, id }
  });
};

this is my reducer这是我的减速机

import { SET_ALERT, REMOVE_ALERT } from '../actions/types';

const initialState = [];

export default function(state = initialState, action) {
  const { type, payload } = action;
  switch (type) {
    case SET_ALERT:
      return [...state, payload];
    case REMOVE_ALERT:
      return state.filter(alert => alert.id !== payload);
    default:
      return state;
  }
}

this is my action types这是我的动作类型

export const SET_ALERT = 'SET_ALERT';
export const REMOVE_ALERT = 'REMOVE_ALERT';

This is my component I want to use my function in这是我的组件,我想在其中使用我的功能

import React, { Fragment, useState } from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { setAlerts } from '../../actions/alert';

export const Register = props => {
  const [formData, setFormData] = useState({
    name: '',
    email: '',
    password: '',
    password2: ''
  });

  const { name, email, password, password2 } = formData;

  const onChange = e =>
    setFormData({ ...formData, [e.target.name]: e.target.value });

  const onSubmit = async e => {
    e.preventDefault();
    if (password !== password2) {
      props.setAlerts('Password dont match', 'danger');
    } else {
      console.log('Succes');
    }
  };
  return (
    <Fragment>
      <section className='container'>
        <h1 className='large text-primary'>Sign Up</h1>
        <p className='lead'>
          <i className='fas fa-user'></i> Create Your Account
        </p>
        <form className='form' onSubmit={e => onSubmit(e)}>
          <div className='form-group'>
            <input
              type='text'
              placeholder='Name'
              name='name'
              value={name}
              onChange={e => onChange(e)}
              required
            />
          </div>
          <div className='form-group'>
            <input
              type='email'
              placeholder='Email Address'
              name='email'
              value={email}
              onChange={e => onChange(e)}
            />
            <small className='form-text'>
              This site uses Gravatar so if you want a profile image, use a
              Gravatar email
            </small>
          </div>
          <div className='form-group'>
            <input
              type='password'
              placeholder='Password'
              name='password'
              minLength='6'
              value={password}
              onChange={e => onChange(e)}
            />
          </div>
          <div className='form-group'>
            <input
              type='password'
              placeholder='Confirm Password'
              name='password2'
              minLength='6'
              value={password2}
              onChange={e => onChange(e)}
            />
          </div>
          <input type='submit' className='btn btn-primary' value='Register' />
        </form>
        <p className='my-1'>
          Already have an account? <Link to='/login'>Sign In</Link>
        </p>
      </section>
    </Fragment>
  );
};


export default connect(null, { setAlerts })(Register);

Try this:尝试这个:

const mapDispatchToProps = dispatch => {
    return {
      setAlerts : (masg, alertType) => { dispatch(setAlerts (masg, alertType)) }
    }
}


export default connect(null,mapDispatchToProps)(Register)

When using Redux, do not export this way in the component.使用 Redux 时,不要在组件中这样导出。

Change below:更改如下:

export const Register = props => {...}

To this:对此:

const Register = props => {...}

You will need to modify this component's import in the App.js.您需要在 App.js 中修改此组件的导入。

Change below:更改如下:

import { Register } from '...';

To this:对此:

import Register from '...';

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

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