0

I have added JavaScript file to my Node.js application and imported, but I cannot add the function from my JavaScript file to the click event of a button.

import React from "react";

import styles from "./Homeapp.css";
import Homee from "./Homee";

const Home = ({ handleLogOut }) => {
  return (
    <div id="sidebar">
      <div class="toggel-btn" onClick="toggelsidebar()">
        <span></span>
        <span></span>
        <span></span>
      </div>
      <ul>
        <li>Home</li>
        <li>About</li>
        <li>Contact Us</li>
      </ul>
    </div>
  );
};
1
  • You want to add script .js? Commented Jan 21, 2021 at 15:30

2 Answers 2

1

You need to import the function you want to call, and then pass the function and not a string to your JSX components onClick prop:

import { toggelsidebar } from "./someJsModule"

and

<div class="toggel-btn" onClick={toggelsidebar}>

This assumes that someJsModule.js exports toggelsidebar in the first place.

Sign up to request clarification or add additional context in comments.

Comments

0

If you file is Homee please add extension.

import Homee from "./Homee.js"

Comments

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.