星座水合策略 `client:only=”react”` 在 React JS 中无法正常工作。

huangapple go评论93阅读模式
英文:

astro hydration strategy `client:only="react"` not working with react js

问题

  1. // quote.tsx
  2. import React, { useEffect, useState } from "react";
  3. const Quote = () => {
  4. const [quote, setQuote] = useState("");
  5. async function _fetchQuote() {
  6. const res = await fetch('https://api.adviceslip.com/advice');
  7. const data = await res.json();
  8. const quote = data['slip']['advice'];
  9. setQuote(quote);
  10. }
  11. useEffect(() => {
  12. _fetchQuote();
  13. }, []);
  14. return <h1>
  15. <span>Quote: </span>
  16. <span>{quote}</span>
  17. </h1>;
  18. }
  19. export default Quote;
英文:

This is my simple react component that is fetching the quote on the client side.

  1. // quote.tsx
  2. import React, { useEffect, useState } from &quot;react&quot;;
  3. const Quote = () =&gt; {
  4. const [quote, setQuote] = useState(&quot;&quot;);
  5. async function _fetchQuote() {
  6. const res = await fetch(&#39;https://api.adviceslip.com/advice&#39;);
  7. const data = res.json();
  8. const quote = data[&#39;slip&#39;][&#39;advice&#39;];
  9. setQuote(quote);
  10. }
  11. useEffect(() =&gt; {
  12. _fetchQuote();
  13. }, []);
  14. return &lt;h1&gt;
  15. &lt;span&gt;Quote: &lt;/span&gt;
  16. &lt;span&gt;{quote}&lt;/span&gt;
  17. &lt;/h1&gt;;
  18. }
  19. export default Quote;

This is my astro component, where I have a basic layout and I want to show react component.

  1. // quote-page.astro
  2. ---
  3. import Base from &quot;../layouts/Base.astro&quot;;
  4. import Quote from &quot;./components/quote&quot;;
  5. ---
  6. &lt;Base title=&quot;Qutoes&quot;&gt;
  7. &lt;Quote client:only=&quot;react&quot; /&gt;
  8. &lt;/Base&gt;

I am using client:only=&quot;react&quot; as the docs said but it seems to give me this error

  1. 06:39:24 PM [astro] reload /src/pages/quote-page.astro
  2. error Unable to render `Quote`. When using the `client:only` hydration strategy, Astro needs a hint to use the correct renderer.
  3. Hint:
  4. Did you mean to pass `client:only=&quot;react|preact|solid-js|vue|svelte|lit&quot;`? See https://docs.astro.build/en/reference/directives-reference/#clientonly for more information on client:only
  5. File:
  6. /home/.../src/page/quote-page.astro

Although I gave the value react as well it does not seem to be working I tried changing the file extension from quote.tsx to quote.jsx not worked.

My astro.config.ts is as follows

  1. import { defineConfig } from &#39;astro/config&#39;;
  2. import sitemap from &#39;@astrojs/sitemap&#39;; // https://astro.build/config
  3. import vercel from &quot;@astrojs/vercel/serverless&quot;;
  4. // https://astro.build/config
  5. export default defineConfig({
  6. site: &quot;https://url&quot;,
  7. markdown: {
  8. syntaxHighlight: &quot;prism&quot;
  9. },
  10. integrations: [sitemap()],
  11. output: &#39;server&#39;,
  12. adapter: vercel(),
  13. });

My dependencies in package.json

  1. &quot;devDependencies&quot;: {
  2. &quot;astro&quot;: &quot;^2.3.0&quot;,
  3. &quot;path-browserify&quot;: &quot;^1.0.1&quot;
  4. },
  5. &quot;dependencies&quot;: {
  6. &quot;@astrojs/react&quot;: &quot;^2.1.1&quot;,
  7. &quot;@astrojs/rss&quot;: &quot;^0.2.0&quot;,
  8. &quot;@astrojs/sitemap&quot;: &quot;^0.1.0&quot;,
  9. &quot;@astrojs/vercel&quot;: &quot;^3.2.4&quot;,
  10. &quot;sass&quot;: &quot;^1.52.1&quot;
  11. }

答案1

得分: 2

astro.config.js 文件中,在 integrations 部分传递 react() 也是。

参考这里

英文:

In astro.config.js, under integrations pass react() also.

Reference here.

huangapple
  • 本文由 发表于 2023年4月19日 21:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定