简体   繁体   English

如何在 React 中将映射变量从父级传递给子级

[英]How do I pass a mapping variable from parent to child in React

So I have just started learning React and its been fun so far, however I'm trying to split my components into sections for cleaner prettier code.所以我刚刚开始学习 React 并且到目前为止它很有趣,但是我正在尝试将我的组件分成多个部分以获得更清晰更漂亮的代码。

This is my component -这是我的组件-

class Papers extends React.Component {
       // Constructor 
       constructor(props) {
        super(props);
   
        this.state = {
            items: [],
            DataisLoaded: false
        };
    }
   
    // ComponentDidMount is used to execute the code 
    componentDidMount() {


        const randomFilmId = Math.floor(Math.random() * 10) + 1

        const url ="#"
        fetch(url)

            .then((res) =>  res.json())

            .then((json) => {


                this.setState({
                    items:json,
                    DataisLoaded: true
        
                });
            })

            .catch ((err) => { 
                console.log("something went wrong ", err) 
            });
    }
    render() {
        const { DataisLoaded, items } = this.state;
        if (!DataisLoaded) return <div>
            <h1> Pleses wait some time.... </h1> </div> ;
   
        return (
            <div className="auth-box">
            <div className="auth-card" key="{item.id}">
            <div className = "App">
                <h1> Fetch data from an api in react </h1>  {
                    items.map((item) => ( 
                <ul key = { item.id } >
                     <li>  User_Name: { item.first_name }, </li>
                     <li>     Full_Name: { item.middle_name },  </li>
                     <li>  User_Email: { item.last_name } </li>
                        </ul>
                    ))
                }
           </div>
           </div>
           </div>
    );
}
}



export default Papers;

But if I want to put the following piece oof code in a new class,但是,如果我想将以下 oof 代码放入新的 class 中,

 <div className="auth-box">
        <div className="auth-card" key="{item.id}">
        <div className = "App">
            <h1> Fetch data from an api in react </h1>  {
                items.map((item) => ( 
            <ul key = { item.id } >
                 <li>  User_Name: { item.first_name }, </li>
                 <li>     Full_Name: { item.middle_name },  </li>
                 <li>  User_Email: { item.last_name } </li>
                    </ul>
                ))
            }
       </div>
       </div>
       </div>

How would I pass all the props into the new class?我如何将所有道具传递到新的 class 中? I'm sorry if this a basic beginner question.如果这是一个基本的初学者问题,我很抱歉。

Appreciate all the help and support感谢所有的帮助和支持

You can pass down props to child components very easily.您可以非常轻松地将道具传递给子组件。

<Auth userDetails={this.state.items} />

And iside the new class component you can access them as除了新的 class 组件之外,您可以将它们访问为

this.props.userDetails

It is pretty simple actully.其实很简单。 You just do你只是做

class AuthComponent extends React.Component {
    render () {
        return (
            <div className="auth-box">
                <div className="auth-card" key="{item.id}">
                    <div className = "App">
                        <h1> Fetch data from an api in react </h1>
                        {
                            this.props.items.map((item) => ( 
                                <ul key = { item.id } >
                                    <li>  User_Name: { item.first_name },</li>
                                    <li>     Full_Name: { item.middle_name },  </li>
                                    <li>  User_Email: { item.last_name } </li>
                                </ul>
                            ))
                        }
                    </div>
                </div>
            </div>
        );
    }
}

and in parent do <AuthComponent items={items} />并在父项中执行<AuthComponent items={items} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React 中将道具从孩子传递给父母? - How do I pass props from child to parent in React? 如何将 React 道具从父母传递给孩子,再传递给另一个孩子? - How do I pass a React prop from Parent to Child, to another Child? 如何将值从父级传递给子级,然后对其进行编辑并将其传递回 React? - How do I pass a value from parent to child, then edit it and pass it back in React? 如何在AngularJS中将变量从父范围传递给子范围 - How do I pass a variable from the parent scope to a child scope in AngularJS 我如何将来自父母的道具作为上下文传递给它的孩子? - How do i pass a prop from a parent as context to its child? 如何将参数从子函数传递给父reactjs - How do I pass arguments from a child function to a parent reactjs 如何在 React 中将 state 从孩子传递给父母? - How to pass a state from a child to a parent in React? 如何在反应中将道具从父母传递给孩子? - How to pass props from parent to child in react? 在React.js的父组件中使用react-router时,如何使用react context API将数据从父组件传递到子组件? - How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js? 从父母到孩子反应通行证 - React pass from parent to child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM