【问题标题】:Using functions in react native render在 React Native 渲染中使用函数
【发布时间】:2023-01-04 22:29:50
【问题描述】:

我有 2 个函数,分别称为 isTrueisFalse

export function isTrue(param) {
  return param === true;
};

export function isFalse(param) {
  return param === false;
};

我在 React Native 中有几个按钮,它们从禁用状态更改为 true 或 false。我可以这样做:

<Button
  text="This is a button"
  onPress={handlePress}
  isDisabled={var === true}
/>

但现在我正在使用这样的功能:

<Button
  text="This is a button"
  onPress={handlePress}
  isDisabled={isTrue(var)}
/>

将 console.log 添加到“isTrue”函数时,我看到它被调用了很多次(每次重新渲染)。

这对性能不利吗?我没有看到任何性能问题,但它是一个简单的应用程序(目前),而且我认为此函数中的计算并不困难。

我打算添加更多功能,如isNullisNotNull。它仅供我自己使用,var === null 的工作方式类似于 isNull(var),但对我而言它更易于阅读。如果可能存在任何性能问题,我会再次切换回来。

【问题讨论】:

    标签: javascript react-native


    【解决方案1】:

    你应该像这样使用它。

    <Button
    text="This is a button"
    onPress={handlePress}
    isDisabled={()=>isTrue(var)}
    >
    

    【讨论】:

    • 为什么?那会改变什么?你能澄清一下你的答案吗?
    猜你喜欢
    • 2018-12-24
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 2016-03-29
    • 2019-09-11
    • 2019-08-14
    相关资源
    最近更新 更多