[英]How pass API info from a react component to another?
i am fighting with a react app with a movie API ( https://developers.themoviedb.org ) hahaha.我正在与带有电影 API ( https://developers.themoviedb.org ) 的 React 应用程序作斗争,哈哈哈。 I have a list component with movie and tv cards.
我有一个包含电影和电视卡的列表组件。 I map a list state and put the info in my cards (id, title and poster).
我映射了一个列表状态并将信息放在我的卡片中(id、标题和海报)。 When i click in my card, the site must show movie information.
当我点击我的卡片时,网站必须显示电影信息。 So, i have another component named movies.jsx and there i have another three components: hero.jsx (who contains a img from the movie), menuHero.jsx (a menu) and movieInfo.jsx (that contains the title, a little desc from the movie and blabla).
所以,我有另一个名为 movies.jsx 的组件,还有另外三个组件: hero.jsx(包含电影中的 img)、menuHero.jsx(菜单)和 movieInfo.jsx(包含标题,有点来自电影和blabla的描述)。 I need fill this three components with the api info.
我需要用 api 信息填充这三个组件。 My component movies.jsx show me the id (i put that in a < h2> just for see if its working) but i don't find a way to give them the api info to my another three child components.
我的组件movies.jsx 向我展示了id(我把它放在< h2> 中只是为了看看它是否工作)但我没有找到一种方法将api 信息提供给我的另外三个子组件。
Also, the movieData its empty.此外,movieData 为空。 So, i dont know what im doing wrong.
所以,我不知道我做错了什么。
Here is my code:这是我的代码:
const Movies = props => {
const [movieData, setMovieData] = useState([]);
const { match } = props;
const movieId = match.params.id;
useEffect(() => {
axios.get(`https://api.themoviedb.org/3/movie/${movieId}?api_key=${api-key}`)
.then(res => {
setMovieData(res.data);
console.log(movieData)
}).catch(error => console.log(error))
}, []);
return(
<div className="container-section">
<h2>{match.params.id}</h2>
<Hero />
<MenuHero />
<MovieInfo />
</div>
)
}
export default Movies;
enter code here
You can pass that data as props:您可以将该数据作为道具传递:
return movieData && (
<div className="container-section">
<h2>{match.params.id}</h2>
<Hero movieData={movieData} />
<MenuHero movieData={movieData} />
<MovieInfo movieData={movieData} />
</div>
)
More info: https://reactjs.org/docs/components-and-props.html更多信息: https : //reactjs.org/docs/components-and-props.html
You need to get an API key and movie ID from themoviedb, ${movieId} ${api-key} should be your key which should be stored as an environment variable.您需要从 themoviedb 获取 API 密钥和电影 ID, ${movieId} ${api-key}应该是您的密钥,应该存储为环境变量。 (.env) file and to access that key you'll need to use process.env.(VARIABLE_NAME) to get the desired value.
(.env) 文件并访问该密钥,您需要使用 process.env.(VARIABLE_NAME) 来获取所需的值。 That's why you're not getting any data from your get requests.
这就是为什么您没有从 get 请求中获取任何数据的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.