【发布时间】:2020-07-11 18:26:26
【问题描述】:
我尝试导入聊天机器人上下文以访问当前正在编辑的聊天机器人,但出现了问题。你有什么想法可能是这里的问题吗?
非常感谢!
这是我的 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);
【问题讨论】:
-
这是我的控制台告诉我的:ChatState.js:19 Uncaught TypeError: Cannot destruct property 'currentChatbotInEdit' of 'chatbotContext' 因为它未定义。在 ChatState (ChatState.js:19) 在 renderWithHooks (react-dom.development.js:14825) 在 mountIndeterminateComponent (react-dom.development.js:17505) 在 beginWork (react-dom.development.js:18629) 在 HTMLUnknownElement .callCallback (react-dom.development.js:188) 在 Object.invokeGuardedCallbackDev (react-dom.development.js:237) 在 invokeGuardedCallback (react-dom.development.js:292)
标签: reactjs react-hooks react-state use-context