【问题标题】:React Native return one element to console logReact Native 返回一个元素到控制台日志
【发布时间】:2016-03-19 06:38:42
【问题描述】:

如果我将 new Animated.Value(0) 设置为一个变量,然后将该变量输出到控制台日志,我会得到一个如下所示的永无止境的列表。如何将任何单个元素返回到日志?

{ _children: [],
  x: 
   { _children: 
      [ { _children: 
           [ { _children: 
                [ { _props: 
                     { style: [Circular],
                       onStartShouldSetResponder: [Function],
                       onMoveShouldSetResponder: [Function],
                       onStartShouldSetResponderCapture: [Function],
                       onMoveShouldSetResponderCapture: [Function],
                       onResponderGrant: [Function],
                       onResponderReject: [Function],
                       onResponderRelease: [Function],
                       onResponderStart: [Function],
                       onResponderMove: [Function],
                       onResponderEnd: [Function],
                       onResponderTerminate: [Function],
                       onResponderTerminationRequest: [Function],
                       children: 
                        { '$$typeof': {},
                          type: 
                           { [Function]
                             displayName: 'Text',
                             propTypes: [Object],
                             getDefaultProps: [Object],
                             childContextTypes: [Object],
                             contextTypes: [Object],
                             defaultProps: [Object] },
                          key: null,

【问题讨论】:

    标签: ios reactjs console react-native console.log


    【解决方案1】:

    Animated.Value 是一个具有许多属性的复杂嵌套对象。如果您需要访问单个元素,您可以使用dot notation 来访问。

    Ex:
    let obj = new Animated.Value(0);
    console.log(obj._value);//assuming _value is a valid property of the object
    

    【讨论】:

    • 但是你将如何访问例如这里的 displayName 呢?我尝试了点表示法,但我不知道如何引用这些组件。是像 obj._children._children._children._props 还是什么?我尝试的任何方法似乎都不起作用。
    • 包含在 [ ] 中的东西是数组。数组通过索引访问。我认为应该是obj.x._children[0]._children[0]._children[0]._props.children.children.type.displayName。我不确定到底要解决什么问题?很少有人需要做这样的事情。
    • 好吧,我可能走错了路。例如,我将动画的值设置如下: this.state.Anim.setValue(1);那么如果我想检查值是否为 1 怎么办?或者只是基本返回值。
    • 我不确定我是否有这样做的确切目的。是否要重新设置 Animated.Value 对象的值?如果是这样,您可以使用 setValue 方法来做到这一点。 facebook.github.io/react-native/docs/animated.html
    • 这里基本上需要一个getValue。它被设置为 1,所以我例如想问 If (animvalue==1) {then ....}。我最初想将它设置为 1 如果它是 0 和 0 如果它是 1 但不知道如何获取值来检查这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多