【问题标题】:Is possible to call from a const, a function that stay in a class?是否可以从 const 调用,一个留在类中的函数?
【发布时间】:2018-04-11 22:10:58
【问题描述】:

我想从_signIn const 调用onSignIn 函数。我想知道这是否有可能。我尝试将其称为广告 onSignIn() 或 Login.onSignIn() 但没有任何效果。

const _signIn = values => {
  let email = values.email;
  let password = values.password;
  console.log(email + ' ' + ' ' + password);
  return {email, password};
}

const Login = createReactClass({
  getInitialState: function() {
    return {
      loading: false
    }
  },
  onSignIn: function() {
    var {dispatch} = this.props;
    this.setState({
      loading: true
    });
    dispatch(loginUser(email, password)).then(() => {
      this.setState({
        loading: false
      });
    });
  },

这里调用_signInconst

return (
        <View style={{flex: 1, flexDirection: 'column', margin: 40, justifyContent: 'flex-start'}}>
          <Field keyboardType='email-address' label='Email' component={renderField} name='email'/> 
          <Field keyboardType='default' label='Password' component={renderFieldPass} name='password'/> 
          <TouchableOpacity onPress={handleSubmit(_signIn)} style={{margin: 10, alignItems: 'center'}}>
            <Text style={{
              backgroundColor: 'steelblue', color: 'white', fontSize: 16,
              height: 37, width: 200, textAlign: 'center', padding: 10
            }}>Login</Text>
          </TouchableOpacity>
        </View>
      );

App.js

var App = createReactClass({
  getInitialState() {
    return {}
  },
  render() {
    var renderMainView = () => {
      if (this.props.user_id) {
        return (
          <Main />
        );
      } else {
        return (
          <Login />
        );
      }
    }
    return (
      <View style={{flex: 1}}>
        <StatusBar barStyle="light-content"/>
        {renderMainView()}
        <AlertContainer/>
      </View>
    )
  }
});

【问题讨论】:

  • 问题缺少stackoverflow.com/help/mcve。您没有在任何地方调用 onSignIn,也不清楚在哪里调用 _signIn。
  • XY Problem。要调用非静态函数,您首先需要该类的实例,您可以在该实例上调用该函数。除此之外,我不认为这是正确的方法。你能提供更多关于上下文的细节吗?
  • @estus _signIn 在 onPress={} 下的渲染中被调用,但我需要来自 _signIn 的值才能在 onSignIn 中传递它们
  • 也没有 onPress。请让问题中的代码反映这一点。 MCVE 是代码相关问题的要求。
  • 我明白了。这是XY问题。您不能使用 onSignIn,因为没有可以检索的 Login 实例。 TouchableOpacity 和 Login 都应该有共同的父组件(例如 Auth),它将保持loading 状态并提供更改它的方法(通过将 onLogin、onLogoout 道具传递给嵌套组件等)。这是设计问题,我建议检查一下简单的第三方 auth React 组件的工作原理。

标签: javascript reactjs react-native ecmascript-6


【解决方案1】:

由于您想在_signIn const 中调用onSignIn,因此您需要创建该类的实例并从该实例调用函数。

例如

const _signIn = values => {
  // ... Your Data
  LoginInstance.onSignIn() // call your method here
}

const Login = createReactClass({
    // ... Your Data
  }),

const LoginInstance = new Login()

【讨论】:

  • 这段代码不会做任何有用的事情。 React 类不应该手动实例化。
  • 是的,但这正是@Markus Hayner 特别要求的,需要更多数据才能找到正确的解决方法。
  • 这其实是我想要的。这将返回此错误Cannot read property 'dispatch' of undefined
  • 那将是错误,在您的调度流程中,您可能需要提供更多的洞察力。
【解决方案2】:

像这样定义_signIn

function _signIn (values, onSignIn) {
  let email = values.email;
  let password = values.password;
  console.log(email + ' ' + ' ' + password);
  onSignIn.call(this)
  return {email, password};
}

改变你的渲染():

<TouchableOpacity onPress={() => _signIn.call(this, values, this.onSignIn)} ...>

【讨论】:

  • 这个方法在 onPress 里面给了我values is not defined
  • 您必须将values 替换为您的用户名/密码输入值。
  • values 实际上是输入文本。这不是我创建的变量。
  • 你能发布你的handleSubmit吗?
  • const { handleSubmit } = this.props; 内部渲染
猜你喜欢
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 2023-01-03
相关资源
最近更新 更多