javascript - 如何在运行时将视图添加到本机反应中?
问题描述
我真的很困惑如何在运行时添加(和删除)视图和其他此类组件,
例如在您可以使用的 vanilla JavaScript 中document.querySelector('query-here').appendChild(element);
但是我如何使用本机反应来实现同样的目标?例如:
<Pressable onPress={()=>{addElement(element)}}>
<View>
//add elements here
</View>
我知道如何直接实现它:
<View>
{
[...Array(23)].map((el, index) => {
return(
<View key={index}>
<Text>added new element</Text>
</View>
)});
}
</View>
有人可以指出我正确的方向吗?
解决方案
@cakelover 在这里如何根据组件的状态添加项目和删除项目。
import { Button } from 'react-native';
const [loader, setLoader] = React.useState(false); //donot show loader at initial
const showLoader = isShowLoader => { // based on this function you can add or remove single loader from UI
setLoader(isShowLoader);
}
return (
<View>
{loader && <LoaderComponent/>}
<Button
onPress={() => setLoader(!loader)}
title="Toggle Loader Component"
color="#841584"
/>
</View>
)
如果要添加或删除多个相同的组件,例如列表,则应为此使用项目数组。
推荐阅读
- angular - 模板“带有 Angular 的 ASP.NET Core”在 VS Codespace 中无法开箱即用
- c++ - 如何在 C++17 中使用 CERN Root?
- r - 有没有办法在没有赋值的情况下在 R 中引用返回的数据框的变量?
- python - 从图像中提取带有删除线的文本
- python - 未找到参数“(”,)”的“完成”的反向。尝试了 1 种模式:['finished/(?P
[^/]+)$'] - javascript - 如何选择以某个id结尾的所有元素
- uwp-xaml - 如何在 win2d 中将 CanvasBitmap 缩放到所需的新宽度和高度而不会使图像模糊?
- flutter - Flutter,如何在不使用标签库按钮的情况下更改当前标签栏屏幕
- python - 如何在盈透证券导出账户投资组合信息
- azure - Azure Functions - 发布 local.settings.json