这是我的代码卡在onClick()的地方;
我想使用调度方式更改聊天名称的标题(我正在跟踪一个YouTube channel -聪明的程序员开发的imessage克隆),但该 Action 并未得到调度,但错误 pop 了,
我正在使用Firebase作为后端。
import React from 'react'
import "./SidebarChat.css"
import { Avatar } from '@material-ui/core'
import { useDispatch } from 'react-redux'
import setChat from "./features/chatSlice"
function SidebarChat({id,chatName}) {
const dispatch = useDispatch();
return (
<div
onClick={()=>(
dispatch(setChat({
chatId: id,
chatName: chatName
})))
}
className='sidebarChat'>
<Avatar />
<div className="sidebarChat__info">
<h3>{chatName}</h3>
<p>Last messg</p>
<small>timestamp</small>
</div>
</div>
)
}
export default SidebarChat
这是聊天import { createSlice } from '@reduxjs/toolkit';
export const chatSlice = createSlice({
name: 'chat',
initialState: {
chatId: null,
chatName: null,
},
reducers: {
setChat: (state,action) => {
state.chatId = action.payload.chatId;
state.chatName = action.payload.chatName;
},
},
});
export const { setChat } = chatSlice.actions;
export const selectChatId = (state) => state.chat.chatId;
export const selectChatName = (state) => state.chat.chatName;
export default chatSlice.reducer;
这是redux商店import { configureStore } from '@reduxjs/toolkit';
import userReducer from '../features/userSlice';
import chatReducer from '../features/chatSlice';
export default configureStore({
reducer: {
user: userReducer,
chat: chatReducer,
},
});
我是一个初学者,我已经浏览了每一页,但是我什么也收不到,有人可以帮我吗这是抛出的错误消息
最佳答案
您应该这样导入setChat
。
import { setChat } from "./features/chatSlice"
关于javascript - TypeError : Cannot read property 'type' of undefined functional components,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64539361/