[英]Is there any better approach for change component by using state and props to render?
I'm new to Next.js & ReactJS, recently I have to make component to render different child in parent component by onClick in DOM element to change state. I'm new to Next.js & ReactJS, recently I have to make component to render different child in parent component by onClick in DOM element to change state. so I have an approached to using setState and pass props to choose which child component to be rendered like this:所以我有一种方法可以使用 setState 并传递道具来选择要呈现的子组件,如下所示:
<div className={styles.leftFrame}>
{" "}
<div
className="d-flex flex-column"
style={{
rowGap: "16px",
}}
>
{" "}
<div className={`d-flex flex-column ${styles.whiteFrame}`}>
<div onClick={() => setrightFrame("main")}>
{" "}
<ClassRoomBtn
type="other"
className="CPE326 Coding in AI"
classTime="17:00 น."
/>
</div>
<div>
<button
className="secondaryButton"
style={{ width: "100%" }}
onClick={() => setrightFrame("createClass")}
>
<h5>สร้างห้องเรียน</h5>
</button>
</div>
</div>
</div>
</div>
</div>
{rightFrame === "createClass" && (
<RightFrame page="create" user={user} />
)}
{rightFrame === "main" && <RightFrame page="main" user={user} />}
</div>
I think that if I have more child component to render in parent component, I have to do like this again (by setState for parent to know/ pass props to render different child component) or is there any better way to render because my approach maybe make my project hard to develop furthur.我认为如果我有更多的子组件要在父组件中渲染,我必须再次这样做(通过 setState 让父知道/传递道具以渲染不同的子组件)或者有更好的渲染方法,因为我的方法可能使我的项目难以进一步发展。
You can match the rightFrame
state to the page and simply do:您可以将rightFrame
state 与页面匹配,只需执行以下操作:
<RightFrame page={rightFrame} user={user} />
instead of doing setRightFrame("createClass")
do setRightFrame("create")
to match the page
prop on your <RightFrame />
而不是做setRightFrame("createClass")
做setRightFrame("create")
来匹配你<RightFrame />
上的page
道具
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.