【问题标题】:Using Constructor(props) without Class Component使用没有类组件的构造函数(道具)
【发布时间】:2020-05-25 05:14:06
【问题描述】:

我在 TypeScript 中创建了一个搜索栏,但现在我无法在其中输入任何内容。我看到的所有控制组件教程都建议使用构造函数(props)左右。如果我在我的代码中使用它,我会得到错误。

没有类组件有没有办法使用它?

例如,我正在为我的页面使用 const()。有什么办法可以让这个搜索栏发挥作用吗?

const userSearchPage = () => (
  <div>
    <PermanentDrawerLeft></PermanentDrawerLeft>
    <div className='main-content'>
      <MuiThemeProvider>
        <DropDownMenu >
          <MenuItem style={{ fontSize: "20px" }} primaryText="Search By" />
          <MenuItem value={1} style={{ fontSize: "20px" }} primaryText="First Name" />
          <MenuItem value={1} style={{ fontSize: "20px" }} primaryText="Last Name" />
        </DropDownMenu>
      </MuiThemeProvider>
   <SearchBar
        onChange={() => console.log('onChange')}
        onRequestSearch={() => console.log('onRequestSearch')}
        style={{
          margin: '0 auto',
          maxWidth: 800
        }}
     />
    </div>
  </div>
);

【问题讨论】:

  • 能否也展示一下 SearchBar 组件的代码?
  • @sylar12 它已经存在了 - 我为糟糕的缩进道歉。

标签: javascript html reactjs typescript searchbar


【解决方案1】:

您必须在功能组件中使用 useState 挂钩。

const userSearchPage = () => {
const [value, setValue] = React.useState('');
return (
  <div>
    <PermanentDrawerLeft></PermanentDrawerLeft>
    <div className='main-content'>
      <MuiThemeProvider>
        <DropDownMenu >
          <MenuItem style={{ fontSize: "20px" }} primaryText="Search By" />
          <MenuItem value={1} style={{ fontSize: "20px" }} primaryText="First Name" />
          <MenuItem value={1} style={{ fontSize: "20px" }} primaryText="Last Name" />
        </DropDownMenu>
      </MuiThemeProvider>
      <SearchBar
        onChange={(value) => setValue(value) }
        value={value} 
        onRequestSearch={() => console.log('onRequestSearch')}
        style={{
          margin: '0 auto',
          maxWidth: 800
        }}
      />
    </div>
  </div>
)} ;

【讨论】:

  • 我应该把所有这些都放在一个函数中吗?如果我用你的 sn-p 替换我现有的代码,我会收到多个错误。例如:React Hook "React.useState" is called in function "userSearchPage" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
  • 使用 onRequestSearch,我得到一个错误:Binding element 'onRequestSearch' implicitly has an 'any' type.and this with target:Property 'target' does not exist on type 'string'
  • 如果我将所有这些都放在函数 Search() 中,onRequestSearch 和目标错误仍然存​​在
  • 我做了一些改动,试试这个。
  • 还是不行。我收到React Hook “useState” is called in function “app” which is neither a React function component or a custom React Hook function 类型错误。
【解决方案2】:

功能组件中的道具只是参数!

function SearchBar (props) {
  let searchbartext = document.getElementById('searchbartext')
  searchbartext.addEventListener('change', (e) => {
    props.onChange(e)
  }
  return ( 
    <form onsubmit={(e) => props.onRequestChange(e)}>
      <input id="searchbartext" style={props.style} />
    </form>
  )
}
const UserSearchPage = () => {
  function onRequestSearch (e) {
    console.log.('request search', e)
  }
  function onChange(e) {
    console.log('onChange')
  }
  return (
  {SearchBar({
        onChange:onChange,
        onRequestSearch:onRequestSearch,
        style:{
          margin: '0 auto',
          maxWidth: 800
        }
     })}
  )
}

编辑:您还必须将功能组件调用为函数,而不是像类组件那样使用&lt;&gt; 语法。我在 SearchBar(props) 中使用了术语“props”,但它只是遵循 Reacts 命名约定。您可以轻松地将其替换为 SearchBar(bananas)。

【讨论】:

  • 能否请您帮助我了解如何在现有代码中使用它?如果我按原样编写此函数,然后在我的 const UserSearchPage() 中调用它,我将使用哪些参数?
  • 我在触摸屏上打字,所以我没有全部输入,但我认为这会将我的代码集成到您的解决方案中?问我是否需要更多解释。
  • 不,如果我将此函数复制到我的代码中,我会在 props 和 change 上遇到错误:Type '{ onchange: (e: any) =&gt; any; style: any; }' is not assignable to type 'DetailedHTMLProps&lt;InputHTMLAttributes&lt;HTMLInputElement&gt;, HTMLInputElement&gt;'
  • 感谢您的反馈!我已经更新了我的 sn-p,介意再试一次吗?
  • 不行,还是不行。我在 onChange 上遇到同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 2021-07-24
  • 2018-06-08
相关资源
最近更新 更多