为了账号安全,请及时绑定邮箱和手机立即绑定

如何将我的代码转换为正确的 jsx 格式?

如何将我的代码转换为正确的 jsx 格式?

开心每一天1111 2022-07-08 16:16:20
我是 Reactjs 的新手。这是我的代码。我希望 Link 组件只包装 Card 组件。但是当我的代码是这样的时候,reactjs 让我失望了。我怎样才能正确编写这些代码?const ImageFrame = ({ movieId, movieName, searchWord, image, personMovieId, clickable, clearFavouriteMovie }) => {return (        <div className = "col-sm-3 mt-5 animated fadeInLeftBig ">            { clickable ? /* if clickable props is true --> go movie, else go movie again but with personal movie id ! */                 <Link to={{ pathname:`/movie/${movieId}`, movieName: `${movieName}`, searchWord: `${searchWord}` }}>                    <Card className = "card-box image-frame  ">                        <Card.Img variant="top" src={image} alt="movieImg" />                    </Card>                    </Link> **!!! I WANT Link component stays here !!!**                     {                         clearFavouriteMovie && <button                            className = "mt-3 btn btn-warning"                            onClick = { () => clearFavouriteMovie(movieId)}                        > Bu Filmi Sil </button>                     }                :                 <Link to = {{pathname: `/movie/${personMovieId}`}}>  {/* Person Known For Movies*/}                <Card className = "bg-dark text-light card-box  image-frame " style = {{maxHeight: "500px"}}>                    <Card.Img variant="top" src={image} alt="movieImg" />                </Card>                </Link>            }                    </div>    )
查看完整描述

1 回答

?
莫回无

TA贡献1865条经验 获得超7个赞

if 语句只能返回一个元素,在这种情况下,您尝试返回两个<Link>并将<button>其合并为一个使用 a<React.fragment>


例子


const ImageFrame = ({ movieId, movieName, searchWord, image, personMovieId, clickable, clearFavouriteMovie }) => {


return (

        <div className = "col-sm-3 mt-5 animated fadeInLeftBig ">

            { clickable ? /* if clickable props is true --> go movie, else go movie again but with personal movie id ! */

                 <React.fragment>

                 <Link to={{ pathname:`/movie/${movieId}`, movieName: `${movieName}`, searchWord: `${searchWord}` }}>

                    <Card className = "card-box image-frame  ">

                        <Card.Img variant="top" src={image} alt="movieImg" />

                    </Card>

                    </Link> **!!! I WANT Link component stays here !!!** 

                    { 

                        clearFavouriteMovie && <button

                            className = "mt-3 btn btn-warning"

                            onClick = { () => clearFavouriteMovie(movieId)}

                        > Bu Filmi Sil </button> 

                    }

                </React.fragment>

                : 

                <Link to = {{pathname: `/movie/${personMovieId}`}}>  {/* Person Known For Movies*/}

                <Card className = "bg-dark text-light card-box  image-frame " style = {{maxHeight: "500px"}}>

                    <Card.Img variant="top" src={image} alt="movieImg" />

                </Card>

                </Link>

            }            

        </div>

    )


查看完整回答
反对 回复 2022-07-08
  • 1 回答
  • 0 关注
  • 207 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号