0

I have one function in Action file and need to call it from componenent file with parameters but while calling it, its giving error for function not defined

Component Code :

    handleClick(e){

         addinput({input1:this.refs.inp1.value,input2:this.refs.inp2.value}) // This function I have defined in action file

      }

Action file code :

export function addinput(myval1) {
 return {
   type: 'ADD_INPUT',
   payload: myval1
 };
}

I am getting below error in console

Uncaught ReferenceError: addinput is not defined

Whats I am doing wrong here !!

2
  • If this is a Redux setup you should probably use dispatch to "communicate" the action to the store. The error here is probably due to a missing "import addinput from './path/to/action/file'" statement. Commented Jun 6, 2017 at 9:44
  • I have imported it already in this file : import React from "react"; import {connect} from "react-redux"; import Button from "./Button"; import bindActionCreators from 'redux' import * as TodoActionCreators from "../actions/countActions"; addinput function is in action/counaction file Is there any other thing I am missing Commented Jun 6, 2017 at 9:49

1 Answer 1

1

What you need to do is import the function from action file in your component like

import {addinput} from './path/to/action'

and I am assuming your usiong redux, you can bind the action to props with connect and bindActionCreators function

import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'

...

class MyComponentName extends React.Component{

  handleClick = (e) => {
        this.props.addinput({input1:this.refs.inp1.value,input2:this.refs.inp2.value}) 

      }
}

function mapDispatchToProps(dispatch){
     return bindActionCreators({addInput}, dispatch)
}
export default connect(null, mapDispatchToProps)(MyComponentName)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Shubham !! It was actually defined in other main App component and I missed to dispatch from there!! Its working fine now !! Thank you again !!
Glad to have helped :)
Could your accept this as an answer if it helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.