Deprecated: Smarty::_getTemplateId(): Implicitly marking parameter $template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/Smarty.class.php on line 1048

Deprecated: Smarty_Internal_Data::getTemplateVars(): Implicitly marking parameter $_ptr as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_internal_data.php on line 193

Deprecated: Smarty_Internal_Data::_mergeVars(): Implicitly marking parameter $data as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_internal_data.php on line 203

Deprecated: Smarty_Internal_Template::__construct(): Implicitly marking parameter $_parent as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_internal_template.php on line 149

Deprecated: Smarty_Resource::source(): Implicitly marking parameter $_template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_resource.php on line 175

Deprecated: Smarty_Resource::source(): Implicitly marking parameter $smarty as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_resource.php on line 175

Deprecated: Smarty_Resource::populate(): Implicitly marking parameter $_template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_resource.php on line 199

Deprecated: Smarty_Template_Source::load(): Implicitly marking parameter $_template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_template_source.php on line 158

Deprecated: Smarty_Template_Source::load(): Implicitly marking parameter $smarty as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_template_source.php on line 158

Deprecated: Smarty_Internal_Resource_File::populate(): Implicitly marking parameter $_template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_internal_resource_file.php on line 28

Deprecated: Smarty_Internal_Resource_File::buildFilepath(): Implicitly marking parameter $_template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_internal_resource_file.php on line 101
コンポーネントをインポートする手順|初心者から学ぶReact.js入門

コンポーネントをインポートする手順

React.jsでの開発の際は、コンポーネントをファイルに分けていくのが一般的です。自身で1からコンポーネントを作り、それらを各所で活用したり、すでに誰かが開発したコンポーネントをインストールして使うこともあります。

他のコンポーネントを取り込むことをインポートといいます。

基本の書き方

コンポーネントをインポートするには、呼び出すファイルの冒頭にimport文を書きます。

import コンポーネント名 from 'ファイルパス';

コンポーネントをインポートする例

例えば、componentsフォルダにButton.jsというファイルがあり、Buttonコンポーネントをインポートしたい場合は、次のように書きます。

import Button from 'components/Button';

const App = () => (
	<div>
		<Button />
	</div>
);

外部公開されているコンポーネントもインポートできる

外部公開されているコンポーネントは、次の手順でインポートして使います。

①パッケージマネージャーでコンポーネントをインストールする

npmなどのパッケージマネージャーで、外部公開されているコンポーネントをローカルPCにインストールします。

npm install コンポーネント名
npm install react-my-component

②コンポーネントを使いたいファイルでインポートの記述を書く

インストールされたコンポーネントは、自身でエクスポートしたコンポーネントファイルを読み込むのと同じようにインポートできます。

import ReactMyComponent from 'フォルダ/コンポーネント名';
import ReactMyComponent from 'components/react-my-component';