如何解决ts2339“ {}”
反应路由器接收代码
import React from "react";
import { Mutation } from "react-apollo";
import { RouteComponentProps } from "react-router-dom";
import { toast } from "react-toastify";
import { LOG_USER_IN } from "../../sharedQueries.local";
import { verifyPhone,verifyPhoneVariables } from "../../types/api";
import VerifyPhonePresenter from "./VerifyPhonePresenter";
import { VERIFY_PHONE } from "./VerifyPhoneQueries";
interface IState {
verificationKey: string;
phoneNumber:string;
}
interface IProps extends RouteComponentProps<any> {}
class VerifyMutation extends Mutation<verifyPhone,verifyPhoneVariables> {}
class VerifyPhoneContainer extends React.Component<IProps,IState> {
constructor(props: IProps) {
super(props);
if (!props.location.state) {
props.history.push("/");
}
debugger;
this.state = {
phoneNumber: props.location.state.phone,verificationKey: ""
};
}
反应路由器发送代码
import React from "react";
import { Mutation,MutationFn } from "react-apollo";
import { RouteComponentProps } from "react-router-dom";
import { toast } from "react-toastify";
import {
startPhoneVerification,startPhoneVerificationVariables
} from "../../types/api";
import PhoneLoginPresenter from "./PhoneLoginPresenter";
import { PHONE_SIGN_IN } from "./PhoneQueries";
interface IState {
countryCode: string;
phoneNumber: string;
}
class PhoneSignInMutation extends Mutation<
startPhoneVerification,startPhoneVerificationVariables
> {}
class PhoneLoginContainer extends React.Component<
RouteComponentProps<any>,IState
> {
public phoneMutation: MutationFn;
public state = {
countryCode: "+82",phoneNumber: ""
};
public render() {
const { history } = this.props;
const { countryCode,phoneNumber } = this.state;
return (
<PhoneSignInMutation
mutation={PHONE_SIGN_IN}
variables={{
phoneNumber: `${countryCode}${phoneNumber}`
}}
onCompleted={data => {
const { StartPhoneVerification } = data;
const phone = `${countryCode}${phoneNumber}`;
if (StartPhoneVerification.ok) {
toast.success("SMS Sent! Redirecting you...");
setTimeout(() => {
history.push({
pathname: "/verify-phone",state: {
phone
}
});
},2000);
} else {
toast.error(StartPhoneVerification.error);
}
}}
>
{(phoneMutation,{ loading }) => {
this.phoneMutation = phoneMutation;
return (
<PhoneLoginPresenter
countryCode={countryCode}
phoneNumber={phoneNumber}
onInputChange={this.onInputChange}
onSubmit={this.onSubmit}
loading={loading}
/>
);
}}
</PhoneSignInMutation>
);
}
编译失败。属性“ phone”在类型“ {}”上不存在。
请让我知道为什么发生错误。
我要用手机发短信和身份验证。
但是,将电话号码从上一页移至下一页时,会出现以下错误。
我使用翻译器是因为我的英语不好。
解决方法
我解决了
if (!props.location.state) {
props.history.push("/");
}
const {
location: { state }
} = props;
let phoneNumber = "";
if (state) {
phoneNumber = state.phone;
} else {
phoneNumber = "";
}
this.state = {
phoneNumber,verificationKey: ""
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。