reactjs - 为什么无法使用 apikey 从 api 获取数据
问题描述
import React, { Component } from "react";
import Nav from "./Nav";
import SearchArea from "./SearchArea";
import MovieList from "./MovieList";
class App extends Component {
constructor() {
super();
this.state = {
movies: [],
searchTerm: "",
};
this.apiKey = process.env.REACT_APP_API;
}
handleSubmit = (e) => {
e.preventDeafault();
fetch(
`https://api.themoviedb.org/3/search/movie?api_key=${this.apiKey}&query=${this.state.searchTerm}`
)
.then((data) => data.json())
.then((data) => {
console.log(data);
this.setState({ movies: [...data.results] });
});
};
handleChange = (e) => {
this.setState({ searchTerm: e.target.value });
};
render() {
return (
<div className="App">
<Nav />
<SearchArea
handleSubmit={this.handleSubmit}
handleChange={this.handleChange}
/>
<MovieList movies={this.state.movies} />
</div>
);
}
}
export default App;
我尝试从电影 api 进行搜索,当我开始在搜索中键入时,console.log 没有出现任何数据。我已经创建了 .env 文件。当我从搜索框中输入值时,在控制台上它只是刷新我不知道如何
REACT_APP_API=someapicodehere
解决方案
推荐阅读
- python-3.x - 有没有办法将此列表理解更改为正常循环
- powershell - 使用 PowerShell 获取所有未知 AD 对象的所有权
- arm - STM32:简单的 SPI 传输
- strapi - 以编程方式在 Strapi 中添加新条目
- java - 同时在 Firebase 中创建多个用户
- react-native - Reanimated v1 再次为从 1 到 0 到 1 的值设置动画
- python - Python 多处理:工作池与进程
- php - 在 laravel 中存储数组值
- laravel - 如何配置单独的 Laravel 和 Vuejs 应用程序?
- python-3.x - 如何减小 numpy.ndarray 的形状?