1

Im new to react.js. I'd like to render out a component. like this

import styled from "styled-components";
import Panel from "./Panel";
const Menu = () => {
  return (
    <Main>
      <div>
        <h1 onClick={Panel}>(1+3)</h1>
      </div>

but I don't think it's right so how do u render specific component with onClick in react.js ??

1
  • 1
    can you show us what your Panel component look like ? is it a styled component? Commented May 24, 2021 at 4:12

1 Answer 1

1

The code should be self-explanatory. Update me if you don't understand any part of them.

import styled from "styled-components";
import Panel from "./Panel";
import { useState } from "react";

const Menu = () => {
  const [showPanel, setShowPanel] = useState(false);
  const handleOnClick = () => setShowPanel(true);

  return (
    <Main>
      <div>
        <h1 onClick={handleOnClick}>(1+3)</h1>
      </div>
      {showPanel ? <Panel /> : null}
    </Main>
  );
};
Sign up to request clarification or add additional context in comments.

1 Comment

oh that's how it use ! Thanks ! this is so helpful !

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.