【问题标题】:Cannot read property 'toLowerCase' of undefined React无法读取未定义 React 的属性“toLowerCase”
【发布时间】:2020-07-23 15:29:12
【问题描述】:

我刚接触 stackoverflow 并做出反应,我遇到了一些问题。 这是代码。

onSearchChange = (event) => {
  this.setState({ searchfiled: event.target.value })
}

render() {
  const filteredRobots = this.state.robots.filter(robots => {
    return robots.name.toLowerCase().includes(this.state.searchfiled.toLowerCase());
  })
  return(
    <div className='tc'>
      <h1>Robofriends</h1>
      <SearchBox searchChange={this.onSearchChange}/>
      <CardList robots={filteredRobots}/>
    </div>
  );
}

我收到此错误类型错误:无法读取未定义的属性“toLowerCase”。 谁能帮帮我?

【问题讨论】:

  • 能否提供这个组件的其余代码!
  • 给你,谢谢 :) [link]pastebin.com/ah8pSTeF
  • this.state.searchfilled 变量是否已初始化?
  • @JorgeGarcía 是的:D
  • @CarlosSaizOrteu 这里是pastebin.com/dbqHsG95

标签: reactjs undefined lowercase


【解决方案1】:

我知道了,您在过滤器中输入错误。你得到了这个:

const filteredRobots = this.state.robots.filter(robots =>{
return robots.name.toLowerCase().includes(this.state.searchfiled.toLowerCase());
})

应该是这样的:

const filteredRobots = this.state.robots.filter(robot =>{
return robot.name.toLowerCase().includes(this.state.searchfield.toLowerCase());
})

您在使用 searchFiled 时弄错了 searchField。

此外,在过滤功能中,您使用的机器人与导入的“机器人”同名,这可能会导致错误。改用机器人。

【讨论】:

  • 非常感谢,我是新手,我正在努力理解 react 的基础知识。
猜你喜欢
  • 1970-01-01
  • 2021-11-28
  • 2021-04-08
  • 2020-06-24
  • 2018-10-23
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多