2
1

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 1 year has passed since last update.

Next.jsでpropsを使ってテキスト情報を渡す

Last updated at Posted at 2021-12-14

Next.jsでpropsを使ってテキスト情報を渡す方法です。
test.jsでtitleという変数にテキスト情報を詰め込んで、test2.jsに渡します。

test.js

import React from 'react';
import Test2 from './test2';

function  Test() {
  return (
    <div style={{ textAlign: 'center' }}>
      <h1>Test1</h1>
      <Test2 title="from test1 to test2"/>
    </div>
  );
}

export default Test;

title="from test1 to test2"
のところで設定。

test2.js

import React from 'react'

const Test2 = (props) => {

    let title = props.title;

    return (
        <div>
            <p>Test2</p>
            <p>{title}</p>
        </div>
    )
}

export default Test2

###結果
スクリーンショット 2021-12-14 14.36.54.png

無事テキスト情報が渡りました!
ただ、残念ながら値情報は渡せないようです。

値を渡したい場合は、useContextを使用します。
https://qiita.com/Pirosiki_/items/c8648bca23d4dc72287d

###参考にさせていただいたサイト様
https://qiita.com/rio_threehouse/items/7632f5a593cf218b9504

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?