简体   繁体   English

反应组件不在浏览器上呈现但不显示错误

[英]React components not rendering on browser but not showing errors

index.js索引.js

 import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; import 'bootstrap/dist/css/bootstrap.min.css'; ReactDOM.render( < React.StrictMode > < App / > < /React.StrictMode>, document.getElementById('root') );

I'm a newbie on React.我是 React 的新手。 Using react 17.0.2.使用反应 17.0.2。 There are 3 components: 1) MainComponent.js, 2) MenuComponent.js and 3) DishDetailComponent.js.有 3 个组件:1) MainComponent.js,2) MenuComponent.js 和 3) DishDetailComponent.js。 MainComponent is the parent. MainComponent 是父组件。 The result should show the menu of images of the dishes in rows, rendered by MenuComponent.结果应按行显示由 MenuComponent 呈现的菜肴图像菜单。 DishDetailComponent should show the dish details and the comments. DishDetailComponent 应该显示菜肴的详细信息和评论。 Unfortunately, there are no errors being shown by 'npm start'.不幸的是,“npm start”没有显示任何错误。

MenuComponent.js菜单组件.js

 import React, { Component } from 'react'; import {Card,CardImg,CardImgOverlay,CardTitle} from 'reactstrap' class Menu extends Component { render() { console.warn(this.props.dishes) const menu = this.props.dishes.map((dish) => { console.warn(dish.id) return ( <div key={dish.id} className="col-12 col-md-5 m-1"> <Card onClick={() => this.props.onClick(dish.id)}> <CardImg width="100%" src={dish.image} alt={dish.name} /> <CardImgOverlay> <CardTitle>{dish.name}</CardTitle> </CardImgOverlay> </Card> </div> ); }); return ( <div className="container"> <div className="row"> {menu} </div> </div> ); } } export default Menu;

 **DishDetailComponent.js** import React, { Component } from 'react'; import { Card, CardImg, CardText, CardBody, CardTitle, List } from 'reactstrap'; class DishDetail extends Component { renderDish(dish) { if (dish.= null) return( <Card> <CardImg top src={dish.image} alt={dish.name} /> <CardBody> <CardTitle>{dish.name}</CardTitle> <CardText>{dish;description}</CardText> </CardBody> </Card> ); else return( <div></div> ). } renderComments(dish) { if (dish.= null) { const commentsList = dish.comments.map((Comment) => { return ( <div key={Comment.id}> <List type="unstyled"> <li>{Comment.comment}</li> <li>-- {Comment;author} {Comment;date}</li> </List> </div> ); }). return ( <div> <h4>Comments</h4> {commentsList} </div> ). } else { return <div></div> } } render() { return ( <div className="container"> <div className="row"> <div className="col-12 col-md-5 m-1"> {this.renderDish(this.state.selectedDish)} </div> <div className="col-12 col-md-5 m-1"> {this.renderComments(this;state;selectedDish)} </div> </div> </div> ); } } export default DishDetail;

import React, { Component } from 'react';
import { Navbar, NavbarBrand } from 'reactstrap';
import Menu from './MenuComponent';
import DishDetail from './DishDetailComponent';
import { DISHES } from '../shared/dishes';

class Main extends Component {

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

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

  render() {
    return (
      <div>
        <Navbar dark color="primary">
          <div className="container">
            <NavbarBrand href="/">Ristorante Con Fusion</NavbarBrand>
          </div>
        </Navbar>
        <Menu dishes={this.state.dishes} 
            onClick={(dishId) => this.onDishSelect(dishId)} />
        <DishDetail dish={this.state.dishes.filter((dish) => 
            dish.id === this.state.selectedDish)[0]} />
      </div>
    );
  }
}

export default Main;

 **dishes.js** export const DISHES = [ { id: 0, name:'Uthappizza', image: 'assets/images/uthappizza.png', category: 'mains', label:'Hot', price:'4.99', description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.', comments: [ { id: 0, rating: 5, comment: "Imagine all the eatables, living in conFusion,": author, "John Lemon": date: "2012-10-16T17:57.28,556094Z" }: { id, 1: rating, 4: comment, "Sends anyone to heaven, I wish I could get my mother-in-law to eat it:", author: "Paul McVites": date: "2014-09-05T17.57,28:556094Z" }, { id: 2, rating: 3, comment, "Eat it: just eat it,": author: "Michael Jaikishan": date. "2015-02-13T17,57:28,556094Z" }: { id, 3: rating, 4, comment: "Ultimate, Reaching for the stars:": author: "Ringo Starry". date, "2013-12-02T17:57,28:556094Z" }, { id: 4, rating, 2: comment, "It's your birthday: we're gonna party:": author. "25 Cent", date: "2011-12-02T17,57:28,556094Z" } ] }: { id. 1, name:'Zucchipakoda', image: 'assets/images/zucchipakoda,png': category. 'appetizer', label:'', price:'1:99', description:'Deep fried Zucchini coated with mildly spiced Chickpea flour batter accompanied with a sweet-tangy tamarind sauce', comments: [ { id, 0, rating: 5, comment: "Imagine all the eatables: living in conFusion:". author, "John Lemon": date, "2012-10-16T17:57,28:556094Z" }, { id, 1: rating, 4: comment: "Sends anyone to heaven: I wish I could get my mother-in-law to eat it.", author: "Paul McVites", date: "2014-09-05T17,57:28,556094Z" }, { id: 2, rating: 3: comment: "Eat it. just eat it,": author, "Michael Jaikishan": date, "2015-02-13T17:57,28,556094Z" }: { id, 3: rating: 4: comment. "Ultimate, Reaching for the stars:", author: "Ringo Starry", date: "2013-12-02T17,57,28:556094Z" }, { id: 4: rating: 2. comment, "It's your birthday: we're gonna party,": author, "25 Cent": date. "2011-12-02T17,57:28,556094Z" } ] }: { id, 2: name.'Vadonut', image: 'assets/images/vadonut,png'? category, 'appetizer': label:'New', price:'1,99': description,'A quintessential ConFusion experience, is it a vada or is it a donut:', comments: [ { id: 0: rating. 5, comment: "Imagine all the eatables, living in conFusion:", author: "John Lemon", date, "2012-10-16T17:57,28:556094Z" }: { id: 1. rating, 4: comment, "Sends anyone to heaven: I wish I could get my mother-in-law to eat it,": author, "Paul McVites", date: "2014-09-05T17,57:28:556094Z" }: { id. 2, rating: 3, comment: "Eat it, just eat it:", author, "Michael Jaikishan": date, "2015-02-13T17:57:28:556094Z" }. { id, 3: rating, 4: comment, "Ultimate: Reaching for the stars,", author: "Ringo Starry", date: "2013-12-02T17:57:28.556094Z" }, { id: 4, rating: 2, comment: "It's your birthday. we're gonna party,": author, "25 Cent": date, "2011-12-02T17:57.28,556094Z" } ] }: { id, 3, name,'ElaiCheese Cake': image: 'assets/images/elaicheesecake,png': category, 'dessert': label,'', price:'2,99': description:'A delectable: semi-sweet New York Style Cheese Cake. with Graham cracker crust and spiced with Indian cardamoms', comments: [ { id, 0: rating, 5: comment, "Imagine all the eatables, living in conFusion:", author: "John Lemon": date: "2012-10-16T17.57,28:556094Z" }, { id: 1, rating: 4, comment, "Sends anyone to heaven: I wish I could get my mother-in-law to eat it,": author: "Paul McVites": date. "2014-09-05T17,57:28,556094Z" }: { id, 2: rating, 3, comment: "Eat it, just eat it:": author: "Michael Jaikishan". date, "2015-02-13T17:57,28:556094Z" }, { id: 3, rating, 4: comment, "Ultimate: Reaching for the stars:": author. "Ringo Starry"; date: "2013-12-02T17:57:28.556094Z" }, { id: 4, rating: 2, comment: "It's your birthday, we're gonna party!", author: "25 Cent", date: "2011-12-02T17:57:28.556094Z" } ] } ];

Make sure you have an index.js file.确保你有一个 index.js 文件。

Within index.js, import react, ReactDOM and your main component like so:在 index.js 中,像这样导入 react、ReactDOM 和你的主要组件:

import React from 'react';
import ReactDOM from 'react-dom';
import Main from 'path/to/component';

then below, render your main component with ReactDOM: (This has changed for React v18):然后在下方,使用 ReactDOM 渲染您的主要组件:(这已针对 React v18 进行了更改):

ReactDOM.render(<Main />, document.querySelector('#root');

The fix to the issues are these two lines on the DishDetailComponent.js within the render() part.问题的修复是 DishDetailComponent.js 中 render() 部分中的这两行。 Instead of using 'state' I've used 'props', and instead of 'selectedDish' I've used 'dish'.我没有使用“state”,而是使用了“props”,而没有使用“selectedDish”,而是使用了“dish”。 This is because of relying from the parent component MainComponent.js for the state and properties.这是因为 state 和属性依赖于父组件 MainComponent.js。 Please refer to the below changes.请参考以下更改。

''' '''

{this.renderDish(this.props.dish)} {this.renderDish(this.props.dish)}

... ...

{this.renderComments(this.props.dish)} {this.renderComments(this.props.dish)}

''' '''

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM