简体   繁体   English

如何将 API 信息从 React 组件传递到另一个组件?

[英]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.

相关问题 React:如何从一个组件获取API数据并将其传递给另一组件 - React: How to get API data from one component and pass it to another "React 将从 API 获取的数据传递给另一个组件" - React pass fetched data from API to another component React 如何将数据从组件传递到另一个组件 - how to pass data from component to another component in React 如何从API中获取数据,然后在完全加载后将其传递给另一个组件以进行反应? - How to fetch data from API and then pass it to another component in react after it is fully loaded? 如何在 React.js 中将状态/数据从一个组件传递到另一个组件(特别是 riot api) - How to pass state/data from one component to another in React.js (riot api specifically) React - 如何将 API 数据从一个组件传递到另一个 js 文件中的另一个组件? - React - How can I pass API data from one component to another in a different js file? 如何将 API 响应从子组件传递到另一个嵌套组件? - How to pass an API response from a child component to another nested component? 如何在另一个组件上使用来自组件的信息 - How to use the info from a component on another component 如何将 React 组件作为道具传递给另一个组件 - How to pass React Component as props to another component 如何在react js中将值从一个组件传递到另一个组件 - how to pass value from one component to another in react js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM