![](/img/trans.png)
[英]React error : Unhandled Rejection (TypeError): this.state.categories.map is not a function
[英]Why doesn't this code run, and instead sends me this error:(Unhandled Rejection (TypeError): this.state is not a function)
我是初學者,我正在嘗試編寫 React 應用程序作為練習
我期待看到這樣的事情: https : //github.com/aneagoie/robofriends但它給了我這個錯誤Unhandled Rejection (TypeError): this.state is not a function (anonymous function)
import React, {Component} from 'react';
import SearchBox from './SearchBox';
import CardList from './CardList';
import './App.css'
import { robot } from './robot';
class App extends Component {
constructor(){
super()
this.state = {
robot: [],
searchField: ''
}
}
componentDidMount(){
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(users => {this.state({robot:robot})})
}
onSearchChange = (event) => {
this.setState({ searchfield: event.target.value})
}
render(){
const filteredrobots = this.state.robot.filter(
robot =>{
return robot.name.includes(this.state.searchfeild)})
return(<div className ='tc'>
<h1>robot friends</h1>
<SearchBox searchChange = {this.onSearchChange}/>
<CardList robot = {filteredrobots}/></div>)
}
}
export default App;
不明白出了什么問題提前謝謝
問題似乎出在componentDidMount
的功能上。
您嘗試在該函數中設置 the,但您沒有執行 this.setState,而是執行了 this.state。
應該是這樣:
componentDidMount(){
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(users => {this.setState({robot:robot})})
}
此外,當您嘗試設置狀態時,它在那個 componentDidMount 函數中,我看不到任何地方定義了robot
......所以機器人的值將是未定義的。
如果您需要澄清,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.