0

In my Component definition in React, I am appending an eventListener like this:

/* events.js */
export function mouseyDown( event ) {
    console.log( event.target );
}

/* main.js */
import mouseyDown from '../controllers/events.js';
const Topbar = React.createClass ({
    callMouseyDown : function( event ) {
        console.log( event.target );
        mouseyDown(); // actually need to pass event to this method too
    },
    render : function() {
        return (
            <div className="tbIcon" data-action="shareSocial" onClick={ this.callMouseyDown}>G</div>
    )}
});

However, React is giving me this error:

_EventController2.default is not a function

What am I doing wrong?

1 Answer 1

1

Mousedown is not property of created object but function which you have imported. Therefore you need to omit this keyword:

onClick={mouseyDown}

Moreover you need to export your function as default:

export default function mouseyDown( event ) {
    console.log( event.target );
}
// ...
import mouseyDown from '../controllers/events.js';

or use named import:

export function mouseyDown( event ) {
    console.log( event.target );
}
// ...
import {mouseyDown} from '../controllers/events.js';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it doesnt seem to work. I get no error, nothing. Ive renamed the onClick function for clarity.
You are a genius of the highest order. Thank you.

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.