react-native - React Native - 在样式表中使用 useDimensions 钩子
问题描述
您如何在样式表中使用从 RN 中的钩子返回的尺寸?
例如
import { useDimensions } from '@react-native-community/hooks';
const ProfileEditScreen = () => {
const { width, height } = useDimensions().window;
...
const styles = StyleSheet.create({
profileEditContainer__form: {
borderRadius: 15,
height: 40,
width: width - width / 4,
borderColor: Colors.defaultColor,
borderWidth: 1,
textAlign: 'center',
},
...
在上面的例子中,width
是not defined
。
过去(类组件),我曾经有一个顶级的
const { width: WIDTH } = Dimensions.get('window');
用功能组件真的不能再这样做了..有什么建议吗?
解决方案
- 您只能在功能组件中使用钩子。
- 对于您的示例,我仍然建议使用
Dimensions.get('window')
.
推荐阅读
- sql - Microsoft 在查询中使用 ORDER BY 中的 switch 语句时访问更改数据类型
- python - 如何使用 user_passes_test 查看特定页面?
- dafny - 验证 dafny 中的谓词是否适用于所有整数
- python - 计算 pyspark 数据框中的出现次数
- javascript - 随机图像和配对文本选择 javascript
- javascript - 在 Jest 环境被拆除后导入文件
- java - 如何返回一个未知值的 char 数组?
- matplotlib - 单个图形上的多种不同类型的图并将其保存到视频中
- angular - 无法读取未定义的 Observables Ionic 的属性方向
- sql - 案例表达式求和