小编Ash*_*Dey的帖子

防止反应悬念隐藏内容

我用谷歌搜索,观看了很多视频来实现类似 YouTube 加载的功能,如下面的屏幕截图所示。因此,我有基于路线的导航,其中有延迟加载,因此一切正常,但在加载内容时,反应悬念将使组件显示:无,因此它在屏幕上变得不可见,只有后备组件可见

我想让内容保持可见,我将制作一个自定义后备组件,例如 YouTube。

<Suspense fallback={<div>Loading...</div>}>
    {content}
</Suspense>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

lazy-loading reactjs react-suspense

5
推荐指数
1
解决办法
2743
查看次数

如何在react redux的createSlice中使用hook

如何在react @reduxjs/toolkit的createSlice中使用钩子将数据保存在localStorage中每当我更改主题时,我想将数据保存在localStorage中。我知道这可以直接使用 js 代码完成,但我在 useLocalStorage 挂钩方面遇到问题。

import { createSlice } from "@reduxjs/toolkit";
import useLocalStorage from "../hooks/useLocalStorage";

const initialState = {
    theme: true
}

const [settings, setSettings] = useLocalStorage("settings", initialState);


export const settingsSlice = createSlice({
    name: "theme",
    initialState: { value: initialState },
    reducers: {
        setSettings: (state, action) => {
            state.value = action.payload;
        },
        toggleTheme: (state) => {
            const temp = { ...state.value, theme: !state.value.theme };

            //save in localStorage
            setSettings(temp);

            state.value = temp;
        },
    },
});

export const { toggleTheme } = …
Run Code Online (Sandbox Code Playgroud)

local-storage redux react-hooks redux-toolkit

1
推荐指数
1
解决办法
5333
查看次数