【问题标题】:class constructors must be invoked with 'new' error in React-native必须在 React-native 中使用“新”错误调用类构造函数
【发布时间】:2020-01-21 10:59:59
【问题描述】:

所以我正在尝试使用 react native 和 aws-amplify 来构建具有一个代码库的 Web 和移动应用程序。现在我刚刚将withAuthenticator 组件添加到我的应用程序中,该组件在移动部分上运行良好,但是当我在浏览器中运行网络应用程序时,我无法输入身份验证字段,即如果我点击它,光标就会出现并且令人惊讶的是,如果我单击并按住,只要按住鼠标按钮,我就可以突然打字。所以在那之后我尝试创建我的自定义身份验证器,它在移动设备上运行良好,但在网络上它返回错误class constructors must be invoked with 'new'。我的代码在下面,希望有人可以帮助我。谢谢!

  class MySignIn extends SignIn {

    constructor(props) {
      super(props);   
      this.state = {
          username: null,
          password: null,
          error: null
      }
      this.checkContact = this.checkContact.bind(this);
      this.signIn = this.signIn.bind(this);
  }


    render() {
      if (this.props.authState !== 'signIn') {
        return null;
      }
      return(
        <View style={{flex:1, backgroundColor: 'blue'}} behavior="padding">
        <Text>Stuff and Stuff</Text>
    </View>
      );
    }
  }

  export default withAuthenticator(Signout, false, [
    <MySignIn/>,
    <ConfirmSignIn/>,
    <VerifyContact/>,
    <SignUp/>,
    <ConfirmSignUp/>,
    <ForgotPassword/>,
    <RequireNewPassword />
  ]);

【问题讨论】:

  • class MySignIn extends SignIn 这是正确的吗?我通常导入class MySignIn extends React.Component的类
  • 解决了非常感谢
  • 你能告诉我为什么这行得通,因为根据文档应该使用我使用的东西。谢谢

标签: react-native aws-amplify


【解决方案1】:

class MySignIn extends SignIn 这是正确的吗?我通常导入class MySignIn extends React.Component的类

这很有效,因为他们已经从

导入了登录
import { ConfirmSignIn, ConfirmSignUp, ForgotPassword, RequireNewPassword, SignIn, SignUp, VerifyContact, withAuthenticator } from 'aws-amplify-react';

因此这是一个自定义类组件,因此它可以工作

// This is my custom Sign in component
class MySignIn extends SignIn {
  render() {
    ...
  }
} 

这行得通。希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-02
    • 2017-08-17
    • 2021-03-23
    • 2020-11-15
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    相关资源
    最近更新 更多