简体   繁体   English

无法从其他文件定义导入的 object,存储在 state 中并将其传播到 React 中的另一个变量中

[英]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.

相关问题 如何在反应中从另一个文件设置状态变量 - How to set state a variable from another file in react 如何在另一个反应文件中定义局部变量 - How to define local variable in another react file 使用扩展运算符更改 React State Object - Changing a React State Object with Spread Operator 对象分配和传播运算符会改变 React 状态吗? - Object assign and spread operator mutate React state? 反应 useState 钩子影响 state 中使用的默认变量,无论传播运算符,Object.assign 等 - React useState hook affecting default variable used in state regardless spread operators, Object.assign and etc 从另一个React脚本从导入的类访问变量 - Accessing variable from imported class from another React script 从我的反应对象复制状态时,为什么我必须传播我的对象两次 - Why do I have to spread my object twice when copying state from my react object 反应 redux 中的传播状态 - spread state in react redux 在Redux React JS中重定向后无法从存储中检索状态 - Unable to retrieve the state from the store after redirection in Redux React JS 如何从 React + Typescript 中另一个文件中的导入组件调用 function? - How to call a function from an imported component in another file in React + Typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM