简体   繁体   English

IFormContext 只引用一个类型,但在这里用作一个值

[英]IFormContext only refers to a type, but is used as a value here

I am trying to build a validation for a ionic react form.我正在尝试为离子反应形式建立验证。 Before I do that I need to get the state values of the input.在我这样做之前,我需要获取输入的状态值。 I have a field component, which describes the fields, a form structure component and the form page which requires both of the above components.我有一个字段组件,它描述了字段,一个表单结构组件和需要上述两个组件的表单页面。 Below is my code for the form fields:以下是我的表单字段代码:

 import React, {FormEvent} from "react"; import { IErrors } from "./ResetPassword"; import { IonInput} from "@ionic/react"; type Editor = "textbox"; export interface IFieldProps { id: string; name: string; label?: string; editor?: Editor; onIonChange?: any; value?: any; ionChangeValue?: string; } export const ResetField: React.FunctionComponent<IFieldProps> = ({ id, name, label, editor, value, onIonChange })=>{ return ( <div className="form-group"> {label && <label htmlFor={id}>{label}</label>} {editor!.toLocaleLowerCase()=== "textbox" && ( <IonInput id={id} name={name} type="password" value={value} onIonChange={onIonChange} onBlur={()=> (e: FormEvent<HTMLIonInputElement>)=> console.log(e) } className="form-control" /> )} </div> ) }; ResetField.defaultProps = { editor: "textbox" };

Below is the code for the form structure:下面是表单结构的代码:

 import * as React from "react"; import { IonPage, IonHeader, IonInput, IonItem, IonContent, IonLabel, IonToolbar, IonTitle, IonButton } from "@ionic/react"; import '../css/app.scss'; import { ResetField } from "./resetFormFields"; interface IFormProps { render: () => React.ReactNode; } export interface IFormContext extends IFormState { /* Function that allows values in the values state to be set */ setValues: (values: IValues) => void; } /* * The context which allows state and functions to be shared with Field. * Note that we need to pass createContext a default value which is why undefined is unioned in the type */ export const FormContext = (React.createContext < IFormContext) | (undefined > undefined); //<--- I am getting an error saying 'IFormContext' only refers to a type, but is used as a value here export interface IValues { [key: string]: any; } export interface IErrors { [key: string]: string; } export interface IFormState { values: IValues; errors: IErrors; submitSuccess?: boolean; } export interface IFormContext extends IFormState { setValues: (values: IValues) => void; } export class Reset extends React.Component<IFormProps, IFormState> { constructor(props: IFormProps) { super(props); const errors: IErrors = {}; const values: IValues = {}; this.state = { errors, values }; } private haveErrors(errors: IErrors) { let haveError: boolean = false; Object.keys(errors).map((key: string)=> { if(errors[key].length > 0){ haveError = true; } }); return haveError; } private handleSubmit = async ( e: React.FormEvent<HTMLFormElement>): Promise<void> => { e.preventDefault(); if(this.validateForm()){ const submitSuccess: boolean = await this.submitForm(); this.setState({submitSuccess}); } console.log(this.state.values); } private validateForm(): boolean { return true; } private async submitForm(): Promise<boolean> { return true; } public render() { const { submitSuccess, errors } = this.state; return ( <IonPage id="login-registration-page"> <IonHeader> <IonToolbar color="primary"> <IonTitle>Reset Password</IonTitle> </IonToolbar> </IonHeader> <IonContent> <form onSubmit={this.handleSubmit} className="login-registration-form ion-padding" noValidate={true}> <div className="container"> {this.props.render()} <div className="form-group"> <IonButton type="submit" disabled={this.haveErrors(errors)} expand="block" >Submit</IonButton> </div> {submitSuccess && ( <div className="alert alert-info" role="alert"> The form was successfully submitted!! </div> )} {submitSuccess === false && !this.haveErrors(errors) && ( <div className="alert alert-danger" role="alert"> Sorry, an unexpected error has occured </div> )} {submitSuccess === false && this.haveErrors(errors) && ( <div className="alert alert-danger" role="alert"> Sorry, the form is invalid. Please review adjust and try again </div> )} </div> </form> </IonContent> </IonPage> ) } }

And below is my resetForm page code:下面是我的 resetForm 页面代码:

 import React, {useState} from "react"; import { Reset } from "../components/ResetPassword"; import { ResetField } from "../components/resetFormFields"; import { RouteComponentProps } from 'react-router-dom'; import { IonInput} from "@ionic/react"; import '../css/app.scss'; interface OwnProps extends RouteComponentProps {} interface ResetProps extends OwnProps {}; export const ResetForm: React.FunctionComponent<ResetProps> = () => { const [inputCurrentPassword, setCurrentPassword] = useState(''); const [inputNewPawword, setNewPassword] = useState(''); const [inputConfirmPassword, setConfirmPassword] = useState(''); return ( <Reset render={()=> ( <React.Fragment> <div className="alert alert-success" role="alert"> Reset your password to activate your account </div> <ResetField id="currentPassword" name="currentPassword" label="Current Password:" value={inputCurrentPassword} onIonChange={(e: { detail: { value: any; }; }) => setCurrentPassword(e.detail.value!)}/> <ResetField id="newPassword" name="newPassword" label="New Password:" value={inputNewPawword} ionChangeValue="setNewPassword" onIonChange={(e: { detail: { value: any; }; }) => setNewPassword(e.detail.value!)}/> <ResetField id="retypePassword" name="retypePassword" label="Confirm Password:" value={inputConfirmPassword} ionChangeValue="setConfirmPassword" onIonChange={(e: { detail: { value: any; }; }) => setConfirmPassword(e.detail.value!)}/> </React.Fragment> )} /> ) }

In the form structure code, I getting the error saying 'IFormContext' only refers to a type, but is used as a value here .在表单结构代码中,我收到错误消息,指出'IFormContext' only refers to a type, but is used as a value here I don't understand the issue here.我不明白这里的问题。 I get that since IFormContext is an interface, it is a type here.我明白了,因为 IFormContext 是一个接口,它在这里是一种类型。 I wouldn't have issues if it were a class.如果是一门课,我就不会有问题。 But it needs to be an interface here.但是这里需要一个接口。

Your error is due to some misplaced parentheses.你的错误是由于一些错位的括号造成的。 Nothing to do with types vs. interfaces vs. classes, that's all fine.与类型、接口和类无关,这一切都很好。

export const FormContext = (React.createContext < IFormContext) | (undefined > undefined); 

needs to be fixed to需要固定到

export const FormContext = React.createContext<IFormContext | undefined>(undefined); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导出时类型“仅指一种类型,但在此处用作值” - Type “only refers to a type, but is being used as a value here” when exporting “承诺”仅指类型,但在此处用作值 - 'Promise' only refers to a type, but is being used as a value here “&#39;输出&#39;仅指一种类型,但在此处使用了一个值”错误 - "'Output' only refers to a type, but is being used a value here" error “post”仅指一种类型,但在此处用作值 - 'post' only refers to a type, but is being used as a value here &#39;MapEditServiceConfig&#39;仅引用类型,但在此处用作值 - 'MapEditServiceConfig' only refers to a type, but is being used as a value here 元素指的是一个值,但在这里被用作类型 - element refers to a value, but is being used as a type here &#39;ItemIsLoading&#39;on指的是一种类型,但此处被用作值 - 'ItemIsLoading' on refers to a type, but is being used as a value here 指的是一个值,但在这里被用作一个类型 - refers to a value, but is being used as a type here ng-multiselect-dropdown - 'IDropdownSettings' 仅指一种类型,但在此处用作值 - ng-multiselect-dropdown - 'IDropdownSettings' only refers to a type, but is being used as a value here 为什么 TypeScript 中的 'instanceof' 给我错误“'Foo' 仅指一种类型,但在此处用作值。”? - Why does 'instanceof' in TypeScript give me the error "'Foo' only refers to a type, but is being used as a value here."?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM