How do I get a state variable from child to parent?

I am learning React. I have Component structure like this – index.js import React from … Read more How do I get a state variable from child to parent?

I am learning React.
I have Component structure like this –
enter image description here

index.js

import React from "react";
import Button from "./Button/Button"

export default function Index() {

  return (
    <>
    <Button />
    <div>Value of flag in Index.js = {}</div>
    </>
  );
}

Button.js

import React, { useState } from "react";
import "./button.css";

export default function Button(props) {
  const [flag, setFlag] = useState(true);
  const clickHandler = () => {
      setFlag(!flag);   
  };

  return (
      <div className="btn" onClick={clickHandler}>
        Value of flag in Button.js = {flag.toString()}
      </div>
  );
}

My question is "How do I get flag value from Button.js to index.js" ? (child to parent).

Source: React – Stack Overflow


Categories: NewsTags:

Tags:


Leave a Reply

Your email address will not be published. Required fields are marked *