简体   繁体   English

我想调用Action onSubmit Redux表单

[英]I want to call a Action onSubmit redux form

I made action on my actions folder now I want to access "send new message" to my handleSubmit function Below is my action code : 我现在对动作文件夹进行了动作,我想访问“发送新消息”到我的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
  })

now I want to access it on my form "handleSubmit" function Below is my code for message.jsx file 现在我想在我的表单“ 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>
        )
    }
}

Thanks in advance 提前致谢

    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)

The simple way is to import your store and import dispatch from redux. 简单的方法是导入store并从redux导入dispatch Then call store.dispatch(action.sendNewMessage) . 然后调用store.dispatch(action.sendNewMessage) Remember that the store here is your instance of store created using createStore method. 请记住,这里的store是您使用createStore方法创建的商店的实例。 But, ideal way to do it is via using react-redux . 但是,执行此操作的理想方法是使用react-redux

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

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