警告:功能无效,作为一个反应儿童。或如何将ID传递到路由标签中的元素
实际上,我试图渲染DishDetail组件,存在一个错误,例如“警告:功能无效,作为React Child”。因此,我无法在“/菜单/:cindid”下渲染DishDetail组件。
有人可以帮我吗?我试过谷歌搜索。 “ DishDetailComponent”和“ MainComponent”分别下面。多谢!
主组件:
import React, { Component, useContext} from 'react';
import Home from './HomeComponent';
import Menu from './MenuComponent';
import Contact from "./ContactComponent";
import DishDetail from './DishdetailComponent';
import Header from './HeaderComponent';
import About from './AboutComponent';
import Footer from './FooterComponent';
import { Routes, Route, Navigate, useParams} from 'react-router-dom';
import {connect} from 'react-redux';
const mapStateToProps = (state) => {
return {
dishes: state.dishes,
comments: state.comments,
leaders: state.leaders,
promotions: state.promotions
}}
class Main extends Component{
constructor(props) {
super(props);}
render() {
const HomePage =() => {
return(
<Home
dish={this.props.dishes.filter((dish) => dish.featured)[0]}
promotion={this.props.promotions.filter((promo) => promo.featured)[0]}
leader={this.props.leaders.filter((leader) => leader.featured)[0]}
/>
);
}
const DishWithId = ({match}) => {
return(
<DishDetail dish={this.props.dishes.filter((dish) => dish.id === parseInt(match.params.dishId, 10))[0]}
comments={this.props.comments.filter((comment) => comment.dishId === parseInt(match.params.dishId, 10))}
/>
);
};
return (
<div>
<Header/>
<Routes>
<Route path ="/home" element={HomePage()} />
<Route exact path="/menu" element={<Menu dishes={this.props.dishes} />} />
<Route path='/menu/:dishId' element={<DishWithId />} />
<Route exact path="/contactus" element={<Contact />} />
<Route exact path="/aboutus" element={<About leaders={this.props.leaders}/>} />
<Route path="*" element={<Navigate replace to="/home" />} />
</Routes>
<Footer />
</div>
);}}
export default connect(mapStateToProps)(Main);
DishDetailComponent:
import React from 'react';
import { Card, CardImg, CardText, CardBody, CardTitle , Breadcrumb, BreadcrumbItem} from 'reactstrap';
import { Link } from 'react-router-dom';
function RenderDish({dish}) {
console.log('Menu Component render is invoked')
return(
<div className="col-12 col-md-5 m-1">
<Card>
<CardImg width="100%" object src={dish.image} alt={dish.name} />
<CardBody>
<CardTitle>{dish.name}</CardTitle>
<CardText>{dish.description}</CardText>
</CardBody>
</Card>
</div>
);
}
function RenderComments({comments}) {
const comment = comments.map((comment)=> {
return (
<div key={comment.id} className="col-12 col-md-5 m-1">
<p>{comment.comment}</p>
<p>--{comment.author} , {new Intl.DateTimeFormat('en-US', {year: 'numeric', month: 'long', day: '2-digit' }).format(new Date(comment.date))}</p>
</div>
)
});
return(
<div className="col-12 col-md-5 m-1">
<h4>Comment</h4>
{comment}
</div>
);
}
const DishDetail = (props) => {
if (props.dish != null) {
return (
<div className='container'>
<div className='row'>
<Breadcrumb>
<BreadcrumbItem><Link to="/menu">Menu</Link></BreadcrumbItem>
<BreadcrumbItem active>{props.dish.name}</BreadcrumbItem>
</Breadcrumb>
<div className='col-12'>
<h3>{props.dish.name}</h3>
<hr />
</div>
</div>
<div className='row'>
<div className="col-12 col-md-5 m-1">
<RenderDish dish={props.dish} />
</div>
<div className="col-12 col-md-5 m-1">
<RenderComments comments={props.comments} />
</div>
</div>
</div>
);
}
else {
return (
<div></div>
)
}
} export default DishDetail;
Actually, I am trying to render the DishDetail component, there is an error like "Warning: Functions are not valid as a React child." Therefore, I cannot render the DishDetail Component under "/menu/:dishId".
Anyone can help me? I have tried googling it. The "DishDetailComponent" and "MainComponent" are below respectively. Thanks a lot!
The MainComponent:
import React, { Component, useContext} from 'react';
import Home from './HomeComponent';
import Menu from './MenuComponent';
import Contact from "./ContactComponent";
import DishDetail from './DishdetailComponent';
import Header from './HeaderComponent';
import About from './AboutComponent';
import Footer from './FooterComponent';
import { Routes, Route, Navigate, useParams} from 'react-router-dom';
import {connect} from 'react-redux';
const mapStateToProps = (state) => {
return {
dishes: state.dishes,
comments: state.comments,
leaders: state.leaders,
promotions: state.promotions
}}
class Main extends Component{
constructor(props) {
super(props);}
render() {
const HomePage =() => {
return(
<Home
dish={this.props.dishes.filter((dish) => dish.featured)[0]}
promotion={this.props.promotions.filter((promo) => promo.featured)[0]}
leader={this.props.leaders.filter((leader) => leader.featured)[0]}
/>
);
}
const DishWithId = ({match}) => {
return(
<DishDetail dish={this.props.dishes.filter((dish) => dish.id === parseInt(match.params.dishId, 10))[0]}
comments={this.props.comments.filter((comment) => comment.dishId === parseInt(match.params.dishId, 10))}
/>
);
};
return (
<div>
<Header/>
<Routes>
<Route path ="/home" element={HomePage()} />
<Route exact path="/menu" element={<Menu dishes={this.props.dishes} />} />
<Route path='/menu/:dishId' element={<DishWithId />} />
<Route exact path="/contactus" element={<Contact />} />
<Route exact path="/aboutus" element={<About leaders={this.props.leaders}/>} />
<Route path="*" element={<Navigate replace to="/home" />} />
</Routes>
<Footer />
</div>
);}}
export default connect(mapStateToProps)(Main);
The DishDetailComponent:
import React from 'react';
import { Card, CardImg, CardText, CardBody, CardTitle , Breadcrumb, BreadcrumbItem} from 'reactstrap';
import { Link } from 'react-router-dom';
function RenderDish({dish}) {
console.log('Menu Component render is invoked')
return(
<div className="col-12 col-md-5 m-1">
<Card>
<CardImg width="100%" object src={dish.image} alt={dish.name} />
<CardBody>
<CardTitle>{dish.name}</CardTitle>
<CardText>{dish.description}</CardText>
</CardBody>
</Card>
</div>
);
}
function RenderComments({comments}) {
const comment = comments.map((comment)=> {
return (
<div key={comment.id} className="col-12 col-md-5 m-1">
<p>{comment.comment}</p>
<p>--{comment.author} , {new Intl.DateTimeFormat('en-US', {year: 'numeric', month: 'long', day: '2-digit' }).format(new Date(comment.date))}</p>
</div>
)
});
return(
<div className="col-12 col-md-5 m-1">
<h4>Comment</h4>
{comment}
</div>
);
}
const DishDetail = (props) => {
if (props.dish != null) {
return (
<div className='container'>
<div className='row'>
<Breadcrumb>
<BreadcrumbItem><Link to="/menu">Menu</Link></BreadcrumbItem>
<BreadcrumbItem active>{props.dish.name}</BreadcrumbItem>
</Breadcrumb>
<div className='col-12'>
<h3>{props.dish.name}</h3>
<hr />
</div>
</div>
<div className='row'>
<div className="col-12 col-md-5 m-1">
<RenderDish dish={props.dish} />
</div>
<div className="col-12 col-md-5 m-1">
<RenderComments comments={props.comments} />
</div>
</div>
</div>
);
}
else {
return (
<div></div>
)
}
} export default DishDetail;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主页
和dishWithId
是功能组件,因此在两种情况下,此
都是不确定的。另外,您应该完全避免在其他反应组件中声明反应组件。他们应该自己在外面宣布。过滤应发生在父组件中,并且应将过滤结果作为道具传递给路由组件。示例:
最好放弃
Connect
hoc,并在需要访问状态的组件中直接使用use -elector
挂钩。这避免了奇怪的道具钻探问题。示例:
...
组件
其他路由
应用
HomePage
andDishWithId
are function components, sothis
is simply undefined in both cases. Also, you should completely avoid declaring React components within other React components. They should be declared outside on their own. The filtering should occur in the parent component and the filtered result should be passed as a prop to the routed component.Example:
It would be better to ditch the
connect
HOC and use theuseSelector
hook directly in the components that need to access the state. This avoid the odd props drilling issue.Example:
Home
DishDetail
... etc for other routed components ...
App