代码之家  ›  专栏  ›  技术社区  ›  arnoldbird

如何使用非字符串引用而不导致流错误

  •  0
  • arnoldbird  · 技术社区  · 8 年前

    我发现字符串引用是react中的遗留内容: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md

    但我的组件中出现了这个流错误,不知道如何解决它:

    [flow] Cannot assign `c` to `this.loginform` because property `loginform` is missing in `LoginScreen` [1]. (References: [1])
    

    我的组件:

    /**
     * @flow
     */
    
    // ... imports, tcomb form model, etcetera ...
    
    class LoginScreen extends React.Component<*> {
    
      onPress() {
        var creds = this.loginform.getValue();
        orbRequestLogin(this.props.navigation, creds, '/oauth/token');
      }
    
      render() {
        return (
          <Wrap bgcolor="white">
            <Content>
              <KeyboardAvoidingView behavior="padding">
                <Form ref={(c) => { this.loginform = c; }} type={User} options={options} />
    
                <TouchableHighlight style={styles.button} onPress={this.onPress.bind(this)}>
                  <Text style={styles.buttonText}>Log in</Text>
                </TouchableHighlight>
    
              </KeyboardAvoidingView>
            </Content>
          </Wrap>
        );
      }
    }
    
    export default withNavigation(LoginScreen);
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Dimitris Fasarakis Hilliard    8 年前

    您需要为提供注释 loginform 在你的班级里。即:

    class LoginScreen extends React.Component<*> {
        loginform: Form
    

    这在 Class Fields 流程文档的一节。