reactjs - reactjs条件渲染在第二次点击后工作
问题描述
我有三个组件:MyDashboard、MyContent、MyForm。
MyDashboard 组件首先将 MyContent 组件呈现为子组件。我在 MyContent 组件中有一个按钮,单击该按钮应将组件与 MyForm 组件交换。
单击按钮会在第一次单击时闪烁 MyForm 组件,然后直接返回 MyContent 组件。然而,在第二次单击后,MyForm 组件被呈现并保持呈现。
我的仪表板:
function MyDashboard(props) {
const [viewForm, setViewForm] = useState(false); // Don't show the form initially
const handleClick = () => {
setViewForm(!viewForm);
};
return (
<div>
{(() => {
if (viewForm) { // conditionally render the form or the content
return <MyForm {...props} />;
} else {
return (
<MyContent
{...props}
setViewForm={handleClick}
viewForm={viewForm}
/>
);
}
})()}
</div>
);
}
我的内容:
function MyContent(props) {
return (
<p>
<a href="#" onClick={props.setViewForm}> // need to click this twice to render the form.
Show the form
</a>
</p>
);
}
export default MyContent;
我的表格:
function MyForm({ props, location }) {
// form setup
return (
<div>
<Row>
<Form>
//... the form
</form>
</Row>
</div>
);
}
export default MyForm;
解决方案
推荐阅读
- bash - 配置 Kubernetes 以从私有注册表中提取图像时出错
- dataweave - 从有效载荷 Dateweave 中提取文件
- java - 在 Android 中短时间安排时间精确的任务
- python - 寻找最近邻的最终代码问题
- javascript - JavaScript:将字符串数组转换为对象数组
- angular - TypeError:无法读取未定义的属性“电子邮件”(Angular)
- python - 用3写的字典中的键是str?
- git - 如何在提交 ID 不匹配的两个相同存储库之间创建和应用 git 补丁
- typescript - 避免使用泛型定义对象值类型
- mongodb - MongoDB Realm GraphQL - 在自定义解析器中返回多种类型作为自定义有效负载