简体   繁体   English

类型“字符串”上不存在属性“目标”

[英]Property 'target' does not exist on type 'string'

I am making a search bar in Typescript but it is not taking any input.我正在 Typescript 中创建一个搜索栏,但它没有接受任何输入。 The term target on my onChange method keeps giving an error that我的 onChange 方法上的术语目标不断给出错误

Property 'target' does not exist on type 'string'.
export default class userSearchPage extends Component <{}, { searchItem: string}>{
  constructor(props: Readonly<{}>) {
    super(props);
    this.state = {
      searchItem: ''
    };
  }

  render() {
    return (

  <div>
    <PermanentDrawerLeft></PermanentDrawerLeft>
    <div className='main-content'>
      <SearchBar
        onChange={e => {
          this.setState({searchItem: e.target.value})
        }}             

        onRequestSearch={() => console.log('onRequestSearch')}
        style={{
          margin: '0 auto',
          maxWidth: 800
        }}
      />
    </div>
  </div>
  );
}
}

How could I fix this?我怎么能解决这个问题?

Your callback onChange seems like it's already a string.您的回调onChange似乎已经是一个字符串。 So you can use it directly eg所以你可以直接使用它,例如

onChange={e => this.setState({searchItem: e}) }  

I searched for your SearchBar material ui and in the documentation you can see that the onChange doesnt return the event but the actual value as you type.我搜索了您的SearchBar材料 ui,在文档中您可以看到onChange不返回事件,而是在您键入时返回实际值。 Which is a string...hence the error.这是一个字符串...因此错误。

I would also suggest if you are using TypeScript to actually use types.我还建议您是否使用 TypeScript 来实际使用类型。

So for example your onChange should look like所以例如你的 onChange 应该看起来像

onChange={value: string => this.setState({searchItem: value}) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM