android - React Native 视图不起作用,如果语句
问题描述
onValueChange={(itemValue) => {
setSelectedValue(itemValue);
if(itemValue == 'other'){
alert('You Choose: '+itemValue+' Nice')
return(
// The <View> Doesnt Work Why ????
<View style={{backgroundColor:'red',height:5000,width:5000,}}>
<Text>Anas</Text>
</View>
)
}
}}
不工作为什么????如果声明或我可以,我不能把它放进去?我不知道!
解决方案
我整理了一个小沙箱,您可以在其中看到如何实现您想要的示例:https ://codesandbox.io/s/sad-bird-lzzbb
import React, { useState } from "react";
import { Button, Text, View } from "react-native";
export default function App() {
const [selectValue, setSelectedValue] = useState('');
const onValueChange = (itemValue) => {
setSelectedValue(itemValue);
};
return (
<div className="App">
<Button
onPress={() => onValueChange("other")}
title="Click to set value to 'other'"
/>
{selectValue === "other" ? (
<View style={{ backgroundColor: "red", height: 5000, width: 5000 }}>
<Text>Anas</Text>
</View>
) : null}
</div>
);
}
您可以使用该onValueChange
函数在任何您想要的事件中将其作为回调传递(例如onPress
我创建的那个按钮),但是您需要在返回功能组件时返回所有 jsx(或者render
如果您正在使用类,则返回该函数组件)
推荐阅读
- pytorch - PyTorch VAE 无法转换为 onnx
- azure-logic-apps - 我如何从逻辑应用程序中读取 CloudQueueMessage.DequeueCount
- python - 从 CSV 列表中选择到表中
- spring-integration - 无法在 Spring Integration Flow 的建议中删除文件资源
- python - 谷歌 colab python3 名称 cv2 未定义
- node.js - 如何在 node-oidc-provider 中重定向失败的登录尝试
- curve-fitting - MATLAB - 指数(exp2)曲线拟合函数在使用原始方程中的拟合值时不给出与绘图相同的输出
- python - 更简单的 python 等价于 R 风格的 grep,包括多个要匹配的东西
- azure - HDInsight 上的 Ambari API 返回 404
- google-sheets - 使用 SUMIF 函数时,有没有办法接受时间戳值(例如 2/12/2019 19:56:16)作为测试标准的范围?