【问题标题】:I want to call a Action onSubmit redux form我想调用一个 Action onSubmit redux 表单
【发布时间】:2019-11-12 18:04:39
【问题描述】:

我现在对我的操作文件夹进行了操作,我想访问“发送新消息” 到我的 handleSubmit 函数 以下是我的操作代码:

export const types = {
    MESSAGES: {
      SYNC: 'MESSAGES.SYNC',
      NEW: {
        CHANGE: 'MESSAGES.NEW.CHANGE',
        SEND: 'MESSAGES.NEW.SEND'
      }
    }
  }

  export const syncMessages = messages => ({
    type: types.MESSAGES.SYNC,
    messages
  })

  export const changeNewMessage = text => ({
    type: types.MESSAGES.NEW.CHANGE,
    text
  })

  export const sendNewMessage = () => ({
    type: types.MESSAGES.NEW.SEND
  })

现在我想在我的表单“handleSubmit”函数中访问它 下面是我的 message.jsx 文件代码

import React from 'react';
import SubMenu from './SubMenu';
import MessageForm from './form/MessageForm';
import * as action from "../../actions/messages.actions";

export default class Messages extends React.PureComponent {
    handleSubmit = (e) => {
       console.log(e.target.value)
      }

    render() {
        return (
            <section className="page-notifications"> 
                <SubMenu/>
                <MessageForm onSubmit={this.handleSubmit}/>
            </section>
        )
    }
}

提前致谢

【问题讨论】:

    标签: javascript reactjs redux


    【解决方案1】:
        import { sendNewMessage } from './path'
    
    class Messages extends React.PureComponent {
        handleSubmit = (e) => {
           this.props.sendNewMessage();
          }
    
        render() {
            return (
                <section className="page-notifications"> 
                    <SubMenu/>
                    <MessageForm onSubmit={this.handleSubmit}/>
                </section>
            )
        }
    }
    
        const mapDispatchToProps = dispatch => {
          return {
            // dispatching plain actions
            sendNewMessage: () => dispatch(sendNewMessage()),
          }
        }
    
    export default connect(
      null,
      mapDispatchToProps
    )(Messages)
    

    【讨论】:

      【解决方案2】:

      简单的方法是导入你的store 并从redux 导入dispatch。然后拨打store.dispatch(action.sendNewMessage)。请记住,这里的 store 是您使用 createStore 方法创建的商店实例。但是,理想的方法是使用react-redux

      【讨论】:

        猜你喜欢
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多