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

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

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

问题

// quote.tsx

import React, { useEffect, useState } from "react";

const Quote = () => {
    const [quote, setQuote] = useState("");

    async function _fetchQuote() {
        const res = await fetch('https://api.adviceslip.com/advice');
        const data = await res.json();
        const quote = data['slip']['advice'];
        setQuote(quote);
    }

    useEffect(() => {
        _fetchQuote();
    }, []);

    return <h1>
        <span>Quote: </span>
        <span>{quote}</span>
    </h1>;
}

export default Quote;
英文:

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

// quote.tsx

import React, { useEffect, useState } from &quot;react&quot;;

const Quote = () =&gt; {
    const [quote, setQuote] = useState(&quot;&quot;);

    async function _fetchQuote() {
        const res = await fetch(&#39;https://api.adviceslip.com/advice&#39;);
        const data = res.json();
        const quote = data[&#39;slip&#39;][&#39;advice&#39;];
        setQuote(quote);
    }

    useEffect(() =&gt; {
        _fetchQuote();
    }, []);

    return &lt;h1&gt;
        &lt;span&gt;Quote: &lt;/span&gt;
        &lt;span&gt;{quote}&lt;/span&gt;
    &lt;/h1&gt;;
}

export default Quote;

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

// quote-page.astro
---
import Base from &quot;../layouts/Base.astro&quot;;
import Quote from &quot;./components/quote&quot;;
---

&lt;Base title=&quot;Qutoes&quot;&gt;
    &lt;Quote client:only=&quot;react&quot; /&gt;
&lt;/Base&gt;

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

06:39:24 PM [astro] reload /src/pages/quote-page.astro
 error   Unable to render `Quote`. When using the `client:only` hydration strategy, Astro needs a hint to use the correct renderer.
  Hint:
    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
  File:
    /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

import { defineConfig } from &#39;astro/config&#39;;
import sitemap from &#39;@astrojs/sitemap&#39;; // https://astro.build/config

import vercel from &quot;@astrojs/vercel/serverless&quot;;

// https://astro.build/config
export default defineConfig({
  site: &quot;https://url&quot;,
  markdown: {
    syntaxHighlight: &quot;prism&quot;
  },
  integrations: [sitemap()],
  output: &#39;server&#39;,
  adapter: vercel(),
});

My dependencies in package.json

&quot;devDependencies&quot;: {
    &quot;astro&quot;: &quot;^2.3.0&quot;,
    &quot;path-browserify&quot;: &quot;^1.0.1&quot;
},
&quot;dependencies&quot;: {
  &quot;@astrojs/react&quot;: &quot;^2.1.1&quot;,
  &quot;@astrojs/rss&quot;: &quot;^0.2.0&quot;,
  &quot;@astrojs/sitemap&quot;: &quot;^0.1.0&quot;,
  &quot;@astrojs/vercel&quot;: &quot;^3.2.4&quot;,
  &quot;sass&quot;: &quot;^1.52.1&quot;
}

答案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:

确定