gpt4 book ai didi

reactjs - react 使用上下文。无法解构 'currentChatbotInEdit' 的属性 'Object(...)(...)',因为它未定义。我该如何解决?

转载 作者:行者123 更新时间:2023-12-04 11:37:39 25 4
gpt4 key购买 nike

我尝试导入聊天机器人上下文,以便在编辑中访问当前聊天机器人,但出了点问题。你知道这里可能是什么问题吗?

非常感谢!

这是错误消息:
enter image description here

这是我的 chatstate.js 的摘录:

    // import packages
import React, { useReducer, useContext } from "react";
import axios from "axios";

// import local dependencies
import AuthContext from "../../Context/Auth/authContext";
import ChatbotContext from "../../Context/Chatbot/chatbotContext";
import chatReducer from "./chatReducer";
import ChatContext from "./chatContext";
import { ADD_INPUT_TO_CHAT, SESSION_ID_CREATED } from "./chatTypes";

const ChatState = props => {
// get access to auth token
const authContext = useContext(AuthContext);
const { token } = authContext;

// get access to current chatbot in edit
const chatbotContext = useContext(ChatbotContext);
const { currentChatbotInEdit } = chatbotContext;

这是我的 ChatbotState.js 的摘录:
// import packages
import React, { useReducer, useContext } from 'react';
import axios from 'axios';

// import local dependencies
import AuthContext from '../Auth/authContext';
import ChatbotContext from './chatbotContext';
import chatbotReducer from './chatbotReducer';
import {
SUCCESSFUL_CHATBOT_FROM_USER_FETCH,
NEW_QUIZBOT_CREATED
} from './chatbotTypes';

const ChatbotState = props => {
// 1 - Get access to auth token
const authContext = useContext(AuthContext);
const { token } = authContext;

// 2 - Create initial chatbot state
const initialChatbotState = {
userChatbots: [],
currentChatbotInEdit: null
};

// 3 - Get access to the state object and the dispatch function
const [state, dispatch] = useReducer(chatbotReducer, initialChatbotState);

最佳答案

您可能没有为您的根组件提供您的上下文提供程序(如您的情况下的 AuthContextProvider,如果您有的话),我的意思是您的 app.js 或 index.js 取决于您打算提供的组件数量

假设您正在为所有组件提供服务,那么您可以使用 index.js,类似于我在下面的内容;

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import AuthContextProvider from './context/AuthContext';

ReactDOM.render(
<React.StrictMode>
<AuthContextProvider>
<App />
</AuthContextProvider>
</React.StrictMode>,
document.getElementById('root')
);

关于reactjs - react 使用上下文。无法解构 'currentChatbotInEdit' 的属性 'Object(...)(...)',因为它未定义。我该如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60946584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com