首页 > 解决方案 > 如何在“深度 2”层次结构中使用道具

问题描述

我正在尝试将 App.js 文件的道具用于DishDetailComponent.js但我在这两个中间有其他组件,分配说:

应用程序.js

import MenuComponent from './component/MenuComponent'

import './App.css';
class App extends Component {

  constructor(props) {
    super(props);
    this.state = {
      dishes: DISHES
    }
  }


  render() {
    return (
      <div className="app">
        <Navbar dark color="primary">
          <div className="container">
            <NavbarBrand href="/">
              Ristorante Con Fusion
          </NavbarBrand>
          </div>
        </Navbar>
        <MenuComponent
          dishes={this.state.dishes}
        />


      </div>
    );
  }
}

export default App;

菜单组件.js

import React, { Component } from 'react';
import { Card, CardImg, CardImgOverlay, CardText, CardBody, CardTitle } from 'reactstrap';

/* Components */
import DishDetail from './DishDetailComponent'

class MenuComponent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            selectedDish: null
        };
        console.log('Menu Component constructor is invoked');
    }

    componentDidMount() {
        console.log('Menu Component componentDidMount is invoked')
    }

    onDishSelect(dish) {
        this.setState({ selectedDish: dish });
    }

    renderDish(dish) {
        if (dish != null) {
            return (
                <Card >
                    <CardImg width="100%" src={dish.image} alt={dish.name} />
                    <CardBody>
                        <CardTitle>{dish.name}</CardTitle>
                        <CardText>{dish.description}</CardText>
                    </CardBody>
                </Card>
            )
        } else {
            return (
                <div>
                </div>
            )
        }
    }


    render() {
        const menu = this.props.dishes.map((dish) => { /* This is a prop that are provided from App.js 
            in the render() <MenuComponent dishes={}> */
            return (
                <div key={dish.id} className="col-12 col-md-5 m-1">
                    <Card onClick={() => this.onDishSelect(dish)}>
                        <CardImg width="100%" src={dish.image} alt={dish.name} />
                        <CardImgOverlay>
                            <CardTitle><strong>{dish.name}</strong></CardTitle>
                        </CardImgOverlay>
                    </Card>
                </div>
            );
        });



        console.log('Menu component render is invoked')

        return (
            <div className="container">
                <div className="row">
                    {menu}
                </div>
              
                <div className="row col-12 col-md-5 col-lg-5 mr-1">
                    <div>
                        <Card className="">
                            {this.renderDish(this.state.selectedDish)}
                        </Card>
                    </div>
                </div>
            </div>
        );
    }
}

export default MenuComponent;

DishDetailComponent.js

/* Modules */
import React, { Component } from 'react';
import { Card, CardText, CardBody, CardTitle, CardImg } from 'reactstrap';
/* Components */
/* Styles */



class DishDetailComponent extends Component {
    constructor(props) {
        super(props)
        this.state = {
        }
    }

    onDishSelect(dish) {
        this.setState({ selectedDish: dish })
    }

    renderDish(dish) {
        if (dish != null) {
            return (
                <Card >
                    <CardImg width="100%" src={dish.image} alt={dish.name} />
                    <CardBody>
                        <CardTitle>{dish.name}</CardTitle>
                        <CardText>{dish.description}</CardText>
                    </CardBody>
                </Card>
            )
        } else {
            return (
                <div>
                </div>
            )
        }
    }



    render() {

        const comment = this.props.dishes.map((dish) => {
            return ( 
                <div key={dish.key}>
                    <ul className="unorder-list">
                        {dish.comments}
                    </ul>
                </div>
            )
        })

        return (
            <div className="row col-12 col-lg-5 m-1">
                <Card >
                  HEre i have to put the info the name the img and these things 
                </Card>
            </div>
        );
    }
}

export default DishDetailComponent;

这是我尝试做的最后一个组件,但我不知道如何很好地使用道具!

如果你想看我的 git https://gitlab.com/folayao/react-coursera-felipe

标签: javascripthtmlreactjs

解决方案


推荐阅读