12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

props.childrenを渡す方法

Last updated at Posted at 2020-03-08

childrenって何?

<View>
    // ここの中のこと
</View>

タイトルなどのcssは同じで、中の子要素が違うデザインのコンポーネントを作る時はchildrenをpropsとして渡して使います!

やってみた

testComponent/index.jsx


import React, { FC } from "react";
import "./styles.css";

// 型はNodeになります
const Test = ({ children }: { children: Node }) => {
  return (
    <div className="childrenTitle">
      <p>元々Componentに書かれているtext</p>
      {children}
    </div>
  );
};

export default Test;

使う側

import React from "react";
import "./styles.css";
import Test from "./testComponent";

export default function App() {
  return (
    <div className="App">
      <Test>
        <p className="title">--- propsとして渡す値 ---</p>
        <p>こんな感じで子要素を渡せます!</p>
        <p>とっても便利</p>
      </Test>
    </div>
  );
}

結果
スクリーンショット 2020-03-09 3.00.13.png

12
8
1

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
12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?