“app”属性在类型’WritableDraft<{ channelId: null; channelName: null; }>‘上不存在。

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

Property 'app' does not exist on type 'WritableDraft<{ channelId: null; channelName: null; }>'

问题

我正在尝试使用 Tauri、React 和 Redux 构建聊天应用程序。然而,我遇到了这个错误。

Property 'app' does not exist on type 'WritableDraft<{ channelId: null; channelName: null; }>'.

导致这个错误的代码如下:

import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import type { RootState } from '../app/store';

export const appSlice = createSlice({
    name: 'app',
    initialState: {
        channelId: null,
        channelName: null,
    },
    reducers: {
        setChannelId: (state, action) => {
            state.app += action.payload;
        },
    },
});

export const { setChannelId } = appSlice.actions;

export const selectChannelId = (state: RootState) => state.app.channelId;
export const selectChannelName = (state: RootState) => state.app.channelName;

export default appSlice.reducer;

我尝试将 app: null 添加到 initialState 中,像这样:

    initialState: {
        app: null,
        channelId: null,
        channelName: null,
    },

这解决了错误,但我的应用程序无法渲染。

如果需要的话,这是我的 App.tsx

import { useSelector } from "react-redux";
import { selectUser } from "./features/userSlice";

import "./App.css";
import Sidebar from "./Sidebar";
import Chat from "./Chat";

function App() {
  const user: any = useSelector(selectUser);

  return (
    <div className="app">
      {user ? (
        <>
          <Sidebar />
          <Chat />
        </>
      ) : (
        <h2>You need to log in</h2>
      )}

    </div>
  )
}

export default App;

以及我的 store.ts

import { configureStore } from '@reduxjs/toolkit';
import userReducer from '../features/userSlice';
import appReducer from '../features/appSlice';

export const store =  configureStore({
    reducer: {
        user: userReducer,
        app: appReducer,
    }
});

// 推断出 `RootState` 和 `AppDispatch` 类型
export type RootState = ReturnType<typeof store.getState>;

// 推断出的类型: {user: UserState}
export type AppDispatch = typeof store.dispatch;

我已经陷入困境很长时间了,所以任何帮助都将非常有用!

英文:

I am trying to build a chat app with Tauri, React and Redux. I however am stuck on this error.

Property &#39;app&#39; does not exist on type &#39;WritableDraft&lt;{ channelId: null; channelName: null; }&gt;&#39;.

This is the code that produces this error:

import { PayloadAction, createSlice } from &#39;@reduxjs/toolkit&#39;;
import type { RootState } from &#39;../app/store&#39;;

export const appSlice = createSlice({
    name: &#39;app&#39;,
    initialState: {
        channelId: null,
        channelName: null,
    },
    reducers: {
        setChannelId: (state, action) =&gt; {
            state.app += action.payload;
        },
    },
});

export const { setChannelId } = appSlice.actions;

export const selectChannelId = (state: RootState) =&gt; state.app.channelId;
export const selectChannelName = (state: RootState) =&gt; state.app.channelName;

export default appSlice.reducer;

I tried adding app: null to the initialState like so:

    initialState: {
        app: null,
        channelId: null,
        channelName: null,

    },

This solved the error, but my app wouldn't render.

In case it's needed, here is my App.tsx:

import { useSelector } from &quot;react-redux&quot;;
import { selectUser } from &quot;./features/userSlice&quot;;

import &quot;./App.css&quot;;
import Sidebar from &quot;./Sidebar&quot;;
import Chat from &quot;./Chat&quot;;

function App() {
  const user: any = useSelector(selectUser);

  return (
    &lt;div className=&quot;app&quot;&gt;
      {user ? (
        &lt;&gt;
        &lt;Sidebar /&gt;
        &lt;Chat /&gt;
        &lt;/&gt;
      ) : (
        &lt;h2&gt;You need to log in&lt;/h2&gt;
      )}

    &lt;/div&gt;
  )
}

export default App;

And my store.ts:

import { configureStore } from &#39;@reduxjs/toolkit&#39;;
import userReducer from &#39;../features/userSlice&#39;;
import appReducer from &#39;../features/appSlice&#39;;

export const store =  configureStore({
    reducer: {
        user: userReducer,
        app: appReducer,
    }
});

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType&lt;typeof store.getState&gt;;

// Inferred type: {user: UserState}
export type AppDispatch = typeof store.dispatch;

I am stuck on this for a long time now, so all help would be great!

答案1

得分: 1

“app”状态切片的形状是{ channelId: null; channelName: null; }

因此,setChannelId 减速器逻辑应为:

setChannelId: (state, action) =&gt; {
  state.channelId += action.payload;
},
英文:

The shape of the "app" state slice is { channelId: null; channelName: null; }.

So the setChannelId reducer logic should be:

setChannelId: (state, action) =&gt; {
  state.channelId += action.payload;
},

huangapple
  • 本文由 发表于 2023年6月12日 03:08:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452111.html
匿名

发表评论

匿名网友

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

确定