LoginSignup
0
0

More than 1 year has passed since last update.

【React初学者】props親子間の受け渡し

Posted at

通常の受け渡し

Parent.jsx
import { Child } from "../components/child";

export default function App() {
  return (
    <>
      <Child age={20}/>
    </>
  );
}

Child.jsx
export const Child = ({ age }) => {
  return (
    <>
      {age}
    </>
  );
};

children

Parent.jsx
import { Child } from "../components/child";

export default function App() {
  return (
    <>
      <Child>
        <div>children</div>
      </Child>
    </>
  );
}

Child.jsx
export const Child = ({ children}) => {
  return (
    <>
      {children}
    </>
  );
};

親に関数を定義して、子から呼ぶ

Parent.jsx
import { Child } from "../components/child";

export default function App() {
  return (
    <>
      <Child onClick={() => alert("hello")}/>
    </>
  );
}

Child.jsx

export const Child = ({ onClick }) => {
  return (
    <>
      <div onClick={onClick}>関数呼び出し</div>
    </>
  );
};

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0