【问题标题】:How to return values from function - React native如何从函数返回值 - React Native
【发布时间】:2017-07-04 13:12:21
【问题描述】:

如何从 react native 函数返回一个布尔值?

【问题讨论】:

    标签: function react-native return-value


    【解决方案1】:

    可以这样实现:

    export function isJson(str) {
      try {
        JSON.parse(str);
      } catch (e) {
        return false;
      }
      return true;
    }
    

    此函数检查提供的值是否有效JSON

    【讨论】:

      【解决方案2】:

      如果你的函数在一个类中,你可以这样做(检查 toggleBool):

      class IsTrue extends Component
      {
          state = {isTrue: false}
      
          componentWillMount = () =>
          {
              const   newVal = this.toggleBool();
      
              this.setState({isTrue: newVal});
          }
      
          toggleBool = () =>
          {
              if (this.state.isTrue === false)
                  return (true);
              return (false);
          }
      
      
          render = () => <Text>{this.state.isTrue}</Text>
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-30
        • 1970-01-01
        • 2021-10-18
        • 1970-01-01
        • 1970-01-01
        • 2021-02-13
        • 2017-03-20
        • 2022-01-26
        • 1970-01-01
        相关资源
        最近更新 更多