通过ReactJS中的onClick方法调用组件

通过ReactJS中的onClick方法调用组件,reactjs,Reactjs,所以我要做的是通过React中按钮的onClick事件调用组件 我试图调用的组件是CommentForm,我已经在onClick方法中将其作为方法传递,但是该组件没有被调用,我无法继续,非常感谢您的帮助 下面是代码片段 import CommentForm from './CommentForm' . . . const DishDetail=(props)=>{ return( <div className="container

所以我要做的是通过React中按钮的onClick事件调用组件

我试图调用的组件是CommentForm,我已经在onClick方法中将其作为方法传递,但是该组件没有被调用,我无法继续,非常感谢您的帮助

下面是代码片段

import CommentForm from './CommentForm'

. . .

const DishDetail=(props)=>{
        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 className="row m-1">
                            <Button type="submit" value="submit" onClick={()=>CommentForm}><i className="fa fa-pencil"></i> Submit Comment</Button> // <-- component invoked here
                        </div>
                    </div>
                </div>
            </div>
        );

}
import CommentForm from./CommentForm'
. . .
const dishdestail=(道具)=>{
返回(
菜单
{props.dish.name}
{props.dish.name}


CommentForm}>Submit Comment/如果要隐藏或显示组件,应创建包含当前可见性的状态,如下所示:

const Menu = () => {
  return <h3>Hi I am the menu</h3>
}
const App = () => {
  const [isMenuVisible, setIsMenuVisible] = useState(false)
  function handleClick() {
    setIsMenuVisible(!isMenuVisible)
  }
  
  return (
    <>
      <button onClick={handleClick} >{isMenuVisible ? "Hide menu" : "Show menu"}</button>
      {isMenuVisible && <Menu />}
    </>
    )
}
const菜单=()=>{
你好,我是菜单
}
常量应用=()=>{
常量[isMenuVisible,setIsMenuVisible]=useState(false)
函数handleClick(){
setIsMenuVisible(!isMenuVisible)
}
返回(
{isMenuVisible?“隐藏菜单”:“显示菜单”}
{isMenuVisible&&}
)
}
因此,onClick事件必须更改“isVisible”这样的状态。其余操作将由React自身完成。

组件未被“调用”,它需要在父组件的返回方法中作为JSX“呈现”

在本例中,可以使用名为“条件渲染”的方法

在名为showCommentForm的组件中创建状态。此状态为true时,将呈现注释表单。为false时,将不呈现注释表单

const [showCommentForm, setShowCommentForm] = useState(false);
更新按钮以切换状态

 <Button type="submit" value="submit" onClick={()=> setShowCommentForm(prevState => !prevState}><i className="fa fa-pencil"></i> Submit Comment</Button> /
setShowCommentForm(prevState=>!prevState}>提交评论/
现在,通过将组件作为JSX及其道具返回,有条件地呈现组件(我在这里编写了commentFormProps,不确定您需要什么)

{showcomentform&&}
我不是很确定你的风格,但你最终会得到这样的东西

const DishDetail=(props)=>{
    const [showCommentForm, setShowCommentForm] = useState(false); //<-- state to conditionally render your commentForm. the button mutates showCommentForm (toggles it) through setShowCommentForm

    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 className="row m-1">
                            <Button type="submit" value="submit" onClick={()=> setShowCommentForm(prevState => !prevState}><i className="fa fa-pencil"></i> Submit Comment</Button> /
                           {showCommentForm && <CommentForm {...commentFormProps } />} //<-- will only return the CommentForm (or render it) when showCommentForm is true
                    </div>
                </div>
            </div>
        </div>
    );

}
const dishdestail=(道具)=>{
const[showCommentForm,setShowCommentForm]=useState(false);//!prevState}>提交评论/
{showcomentform&&}//
const DishDetail=(props)=>{
    const [showCommentForm, setShowCommentForm] = useState(false); //<-- state to conditionally render your commentForm. the button mutates showCommentForm (toggles it) through setShowCommentForm

    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 className="row m-1">
                            <Button type="submit" value="submit" onClick={()=> setShowCommentForm(prevState => !prevState}><i className="fa fa-pencil"></i> Submit Comment</Button> /
                           {showCommentForm && <CommentForm {...commentFormProps } />} //<-- will only return the CommentForm (or render it) when showCommentForm is true
                    </div>
                </div>
            </div>
        </div>
    );

}