0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

astroファイルでReactコンポーネントが動かない

Last updated at Posted at 2024-10-13

概要

astroファイルでReactコンポーネントが動かなくて苦戦しましたので、対応策を残しておきます。

エラー内容

動かなかった記述
---
import InteractiveCounter from '../components/InteractiveCounter.jsx';
---

<InteractiveCounter />

以下のような状況でした。

  • コンポーネントは表示される
  • ボタンを押しても反応しない
  • console.logも出力されない

対応策

動くようになった記述(コンポーネント部分だけ変更)
- <InteractiveCounter />
+ <InteractiveCounter client:load />

インポートするコンポーネントに client:load を付けただけで解消できました。
クライアントディレクティブというものがあり、その指定が無いと静的なHTMLとして解釈されるようです。
クライアントディレクティブはいくつか種類があり、状況にあわせてコンポーネントの動的化機能を制御できるようです。

おわりに

公式ドキュメントあまり見ていなかったので苦戦してしまいました :innocent:
公式ドキュメントをちゃんと読みましょう。。。

なお、astroの公式ドキュメントはほとんどのページで日本語が用意されているので、大変助かります。

公式ドキュメント

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?