Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

valtio How to replace react's useState and open source hooks #995

Closed
InfernalAzazel opened this issue Nov 11, 2024 · 0 comments
Closed

valtio How to replace react's useState and open source hooks #995

InfernalAzazel opened this issue Nov 11, 2024 · 0 comments

Comments

@InfernalAzazel
Copy link

InfernalAzazel commented Nov 11, 2024

e.g ahooks How to use ahooks together

const { data, run, cancel } = useRequest(getUsername, {
  pollingInterval: 3000,
});

e.g 2 const state2 = proxy({ get rrr(){return data?.message}}); How to use it reasonably

"use client";
import React, { useState } from 'react';
import { proxy, useSnapshot } from 'valtio';
import { useQuery } from '@shined/react-use';

const state = proxy({ count: 0, text: '' });

const fetchData = () =>
    new Promise((resolve) => {
        setTimeout(() => {
            resolve({ message: 'Hello, World!' });
        }, 1000);
    });

export default function Home() {
    const snap = useSnapshot(state);
    const [count, setCount] = useState(0);
    const { loading, data, error, run } = useQuery(fetchData, { manual: true });
    const state2 = proxy({ get rrr(){return data?.message}});

    async function add() {
        if (loading) return;
         await run();
        console.log('更新的文本:', state2.rrr);
    }

    function add2() {
        setCount(3);
        console.log('当前计数:', count);
    }

    return (
        <div>
            <div>
                <span>{snap.text}</span>
                <button onClick={add}>测试</button>
            </div>
            <div>
                <span>{count}</span>
                <button onClick={add2}>测试2</button>
            </div>
        </div>
    );
}
@pmndrs pmndrs locked and limited conversation to collaborators Nov 11, 2024
@dai-shi dai-shi converted this issue into discussion #996 Nov 11, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant