如何选择TextInput onChange事件类型

时间:2019-04-01 00:23:25

标签: typescript react-native

案例代码如下。

import { TextInput } from 'react-native'
import { reduxAction } from './reduxActionPath'

interface AppProps {
  reduxAction: typeof reduxAction
}

class CaseComponent extends React.Component<AppProps> {
  // how to define the event type
  handleChange = (str: string) => (e: whatTheTypeOfE) => {
    this.props.reduxAction({ [str]: e.target.value })
  }

  render(){
    return(
      <TextInput onChange={this.handleChange('userName')} />
      <TextInput onChange={this.handleChange('password')} />
    )
  }
}

它应该安全地检查e的类型,如何选择它?

更新: 现在,我使用onChangeText来解决它,但是我真的很想知道如何启用它的类型?

import { TextInput } from 'react-native'
import { reduxAction } from './reduxActionPath'

interface AppProps {
  reduxAction: typeof reduxAction
}
interface AppState {
  userName: string
  userPwd: string
}

class CaseComponent extends React.Component<AppProps, AppState> {
  render(){
    return(
      <TextInput onChangeText={(userName) => this.setState({ userName })} />
      <TextInput onChangeText={(userPwd) => this.setState({ userPwd })} />
    )
  }
}

0 个答案:

没有答案