在本机反应中扩展通用道具

问题描述 投票:0回答:1

抱歉,如果标题误导了。我似乎找不到合适的标题。我有两个班。

****************Form.tsx

interface Props{}
interface State{}
class Form extends Component<Props, State>{
   protected submit(){
      // contains a code when form submits
   }
}



****************Login.tsx

class LoginForm extends Form{
   render(){
       return(
          <View>
            <Button onClick = {() => this.props.navigation.goBack()}
            <Button onClick= {() => this.submit()} />
          </View>

       );
   }
}

我想这样做

<LoginForm navigation = {someObject} />

但是我不能这样做,因为我似乎找不到解决方法。我只是想知道如何将更多道具传递给LoginForm。

typescript react-native react-navigation
1个回答
0
投票

我找到了一个解决方案,但只有在表单始终扩展且不用作组件的情况下,它才有效。在我的示例中,我从未打算将form用作组件。因此,如果有帮助,这里是一个解决方案。

***********Form.tsx
interface State{}
class Form<P= {}> extends Component<P, State>{

}

***********Login.tsx
interface LoginFormProps{
   navigation: SomeObjectType
}

class Login extends Form<LoginFormProps>{
}

然后我可以做

<LoginForm navigation = {someObject} />
© www.soinside.com 2019 - 2024. All rights reserved.