[英]Unable to define an imported object from a different file , store in a state and spread it in another variable in React
I have defined an object which is the structure of the form which I will use.我已经定义了一个 object,这是我将使用的表单结构。 This file is createCourseForm.js and I am trying to import it in createCourseMain.js and store it in a state.该文件是 createCourseForm.js,我正在尝试将其导入 createCourseMain.js 并将其存储在 state 中。 Then I am trying to spread it in another variable so that I will be using it in a handler function.然后我试图将它传播到另一个变量中,以便在处理程序 function 中使用它。 But I am getting 'firstPageData' is not defined no-undef
and here firstPageData
is the state which I am using to store the object.但是我得到'firstPageData' is not defined no-undef
,这里firstPageData
是我用来存储object的state。 I don't know what's wrong but I guess the state is not updating.我不知道出了什么问题,但我猜 state 没有更新。
https://codesandbox.io/s/cranky-haze-ptzrh?file=/src/createCourseForm.js https://codesandbox.io/s/cranky-haze-ptzrh?file=/src/createCourseForm.js
You need to complete the following steps to export and then import a value from one file to another.您需要完成以下步骤来导出值,然后将值从一个文件导入到另一个文件。
First, export the object on the file you created it (in createCourseForm.js)首先,在您创建的文件上导出 object(在 createCourseForm.js 中)
export const firstPageData = {
// content of the object ...
};
Then import it on the file(s) you will use it (in createCourseMain.js)然后将其导入您将使用它的文件(在 createCourseMain.js 中)
import {firstPageData} from "./createCourseForm.js";
Finally, you need to initialize or set the variables you need to copy this value into.最后,您需要初始化或设置您需要将此值复制到其中的变量。
const [newVariable, setNewVariable] = useState(firstPageData);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.