简体   繁体   English

在 Remix.run 中处理 UI 状态

[英]Handling UI State in Remix.run

I am trying out remix and ohh boy.. I am stuck on creating a simple counter (clicking button increase count)我正在尝试混音和哦男孩..我坚持创建一个简单的计数器(单击按钮增加计数)

I guess I am not supposed to use the useState hook so I tried my luck with loader and action as I believe that this is where it should be handled in Remix我想我不应该使用 useState 钩子,所以我用 loader 和 action 试试运气,因为我相信这是应该在 Remix 中处理的地方

What I have on my component is:我的组件上有:

export default function Game() {
    const counter = useLoaderData();

    return (
        <>
            <div>{counter}</div>
            <div>
                <Form method="post">
                    <button type="submit">click</button>
                </Form>
            </div>
        </>
    );
}

Server:服务器:

import { ActionFunction, LoaderFunction } from 'remix';

let counter: number = 0;

export const loader: LoaderFunction = async () => {
    console.log('game object!', counter);
    return counter;
};

export let action: ActionFunction = async ({ request, params }) => {
    console.log('[action] game object!', ++counter);
    return counter;
};

The code above will have counter always resetting to 0 on every click上面的代码将在每次点击时将计数器始终重置为 0

Looked around some repositories and what I can find are those storing in Cookie/DB/SessionStorage etc, however what if I just want a simple state for my UI?查看了一些存储库,我可以找到存储在 Cookie/DB/SessionStorage 等中的存储库,但是如果我只想为我的 UI 设置一个简单的状态怎么办?

You are supposed to use useState for your client-side state in Remix.应该使用useState作为 Remix 中的客户端状态。

If you clone the remix repository and do a grep -r useState in the remix/examples/ folder, you will find many occurrences of it.如果你克隆 remix 存储库并在remix/examples/文件夹中执行grep -r useState ,你会发现它出现了很多次。

For example you have a simple one in the Route modal example (in app/routes/invoices/$id/edit.tsx ), being used for a form with controlled inputs.例如,您在Route 模式示例中有一个简单的示例(在app/routes/invoices/$id/edit.tsx ),用于具有受控输入的表单。

What Remix does is make it easier to communicate between the client and the server by colocating their codes for the same functionnality, and providing simple ways to perform that communication. Remix 所做的是通过将它们的代码放在一起以实现相同的功能,并提供执行该通信的简单方法,从而使客户端和服务器之间的通信更容易。 This is useful if you need to communicate the data to your server.如果您需要将数据传送到服务器,这很有用。 If it's not the case, then it's totally ok to keep that information only on the client.如果不是这种情况,那么完全可以将该信息仅保留在客户端上。

About server side rendering关于服务器端渲染

Remix also server-side renders the components by default.默认情况下,Remix 还会在服务器端呈现组件。 Which means that it executes your React code on the server to generate the HTML, and sends that with the JavaScript code.这意味着它会在服务器上执行您的 React 代码以生成 HTML,然后将其与 JavaScript 代码一起发送。 That way the browser can display it even before it executes the JavaScript to run the React code on the browser.这样,浏览器甚至可以在执行 JavaScript 以在浏览器上运行 React 代码之前显示它。

This means that in case your code (or a third party library code you use) uses some browser API in you component, you may need to indicate not to server-side render that component.这意味着如果您的代码(或您使用的第三方库代码)在您的组件中使用某些浏览器 API,您可能需要指示不要在服务器端呈现该组件。

There is a Client only components example that demonstrates how to do that.有一个仅客户端组件示例演示了如何执行此操作。 It includes an example with a counter storing its value in the local storage of the browser.它包括一个示例,其中一个计数器将其值存储在浏览器的本地存储中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM