Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:_this2.setState不是函数_Javascript_Reactjs - Fatal编程技术网

Javascript TypeError:_this2.setState不是函数

Javascript TypeError:_this2.setState不是函数,javascript,reactjs,Javascript,Reactjs,我不明白为什么我会犯这个错误。我如何解决它,最重要的是为什么我会得到它 我从API中得到了正确的响应,但我也在一次调用之后得到了错误 class App extends Component { constructor(props) { super(props); this.state = { movies: [] }; } videoSearch(term) { axios.get(`https://api.themoviedb.or

我不明白为什么我会犯这个错误。我如何解决它,最重要的是为什么我会得到它

我从API中得到了正确的响应,但我也在一次调用之后得到了错误

class App extends Component {

  constructor(props) {
    super(props);

    this.state = {
      movies: []
    };
  }

  videoSearch(term) {
    axios.get(`https://api.themoviedb.org/3/search/movie?api_key=${APIkey}&query=${term}&page=1`)
      .then(response => this.setState({movies: response.data.results}))
      .catch(err => console.log(err))
  };


  render() {
    return (
      <div className="App">
        <SearchBar onSearchTermChange={this.videoSearch} />
      </div>
    );
  }
}

export default App;



import React, {Component} from 'react';


class SearchBar extends Component {
    constructor(props) {
        super(props);

        this.state = { term: "" };
    }

    render() {
        return (
        <div className="search-bar">
            <input
            value={this.state.term}
            onChange={event => this.onInputChange(event.target.value)}
            />
        </div>
        );
    }

    onInputChange(term) {
        this.setState({ term });
        this.props.onSearchTermChange(term);
    }
}

export default SearchBar;
类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
电影:[]
};
}
视频搜索(术语){
axios.get(`https://api.themoviedb.org/3/search/movie?api_key=${APIkey}&query=${term}&page=1`)
.then(response=>this.setState({movies:response.data.results}))
.catch(err=>console.log(err))
};
render(){
返回(
);
}
}
导出默认应用程序;
从“React”导入React,{Component};
类搜索栏扩展组件{
建造师(道具){
超级(道具);
this.state={term:”“};
}
render(){
返回(
this.onInputChange(event.target.value)}
/>
);
}
onInputChange(术语){
this.setState({term});
此.props.onSearchTermChange(术语);
}
}
导出默认搜索栏;

基于大量此类问题的猜测:)

尝试:

告诉我它是否有效。如果没有,我将删除答案


我怀疑此是为与您的
应用程序
组件不同的对象调用的。

在构造函数中添加绑定会起作用,但有时会影响可读性

this.videoSearch = this.videoSearch.bind(this);
相反,您也可以将
videoSearch
设置为箭头功能

videoSearch = term => {
  axios.get(`https://api.themoviedb.org/3/search/movie?api_key=${APIkey}&query=${term}&page=1`)
    .then(response => this.setState({movies: response.data.results}))
    .catch(err => console.log(err))
};
videoSearch = term => {
  axios.get(`https://api.themoviedb.org/3/search/movie?api_key=${APIkey}&query=${term}&page=1`)
    .then(response => this.setState({movies: response.data.results}))
    .catch(err => console.log(err))
};