1. <small id='I0Qwi'></small><noframes id='I0Qwi'>

    2. <tfoot id='I0Qwi'></tfoot>

    3. <i id='I0Qwi'><tr id='I0Qwi'><dt id='I0Qwi'><q id='I0Qwi'><span id='I0Qwi'><b id='I0Qwi'><form id='I0Qwi'><ins id='I0Qwi'></ins><ul id='I0Qwi'></ul><sub id='I0Qwi'></sub></form><legend id='I0Qwi'></legend><bdo id='I0Qwi'><pre id='I0Qwi'><center id='I0Qwi'></center></pre></bdo></b><th id='I0Qwi'></th></span></q></dt></tr></i><div id='I0Qwi'><tfoot id='I0Qwi'></tfoot><dl id='I0Qwi'><fieldset id='I0Qwi'></fieldset></dl></div>
      <legend id='I0Qwi'><style id='I0Qwi'><dir id='I0Qwi'><q id='I0Qwi'></q></dir></style></legend>
        <bdo id='I0Qwi'></bdo><ul id='I0Qwi'></ul>

        React Native ScrollView - 如何从另一个子组件按钮滚动到子组件?

        React Native ScrollView - How to scroll to a child component from another child component button?(React Native ScrollView - 如何从另一个子组件按钮滚动到子组件?)
        <legend id='ko8Jp'><style id='ko8Jp'><dir id='ko8Jp'><q id='ko8Jp'></q></dir></style></legend>
          <tfoot id='ko8Jp'></tfoot>
            <tbody id='ko8Jp'></tbody>

          <small id='ko8Jp'></small><noframes id='ko8Jp'>

                • <bdo id='ko8Jp'></bdo><ul id='ko8Jp'></ul>

                  <i id='ko8Jp'><tr id='ko8Jp'><dt id='ko8Jp'><q id='ko8Jp'><span id='ko8Jp'><b id='ko8Jp'><form id='ko8Jp'><ins id='ko8Jp'></ins><ul id='ko8Jp'></ul><sub id='ko8Jp'></sub></form><legend id='ko8Jp'></legend><bdo id='ko8Jp'><pre id='ko8Jp'><center id='ko8Jp'></center></pre></bdo></b><th id='ko8Jp'></th></span></q></dt></tr></i><div id='ko8Jp'><tfoot id='ko8Jp'></tfoot><dl id='ko8Jp'><fieldset id='ko8Jp'></fieldset></dl></div>

                  本文介绍了React Native ScrollView - 如何从另一个子组件按钮滚动到子组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个带有 ScrollView 的结构,它是一个有 5 个孩子的父级

                  I have this structure with a ScrollView, which is a parent with 5 childs

                  带有 ScrollView 的父组件

                  Parent Component with ScrollView

                  • 组件1
                  • 组件2
                  • 组件3
                  • 组件4
                  • 组件5

                  在 Component3 内部,我有一个按钮,按下该按钮应将父组件 ScrollView 滚动到 Component5

                  Inside Component3 I have a button that when pressed should scroll parent component ScrollView to Component5

                  类似的东西

                  首页(父)

                  export default class Home extends React.Component {      
                      renderComments() {
                          return this.state.dataSource.map(item =>
                              <CommentDetail key={item.id} comment={item} />
                          );
                      }
                  
                      render() {
                          return (
                              <ScrollView>      
                                  <Component1 />
                                  <Component2 />
                                  <CentralElements  {...this.state.dataSource} scroll = {this.props.scroll} />
                                  <Component4 />                  
                                  <View>
                                      {this.renderComments()}
                                  </View>
                              </ScrollView>
                          );
                      }
                  }

                  CentralElements (Component3)

                  export default class CentralElements extends React.Component {
                      constructor(props) {
                          super(props);
                      }
                    
                      goToComments= () => {
                          this.props.scroll.scrollTo({x: ?, y: ?, animated: true});
                       };
                  
                      render() {
                          return (
                              <ScrollView horizontal={true}>
                                  <TouchableOpacity onPress={this.goToComments}>
                                      <Image source={require('../../assets/image.png')} />
                                      <Text>Comments</Text>
                                  </TouchableOpacity>
                                  ...
                                  </TouchableOpacity>
                              </ScrollView>
                          );
                      }
                  };

                  评论是Component5,关于如何父滚动的任何想法?我试图弄清楚我错过了什么,因为那是我第一次接触这个.

                  And the Comments are the Component5, any idea on how to the parent scroll? I trying to figure what I'm missing, since thats my first contact with this.

                  推荐答案

                  我做的是..
                  在 component5 中,我在主视图中调用 onLayout,然后将 xy 保存在父组件中.要在单击时在组件 3 中滚动到它,我调用父函数,该函数使用 scrollview ref 滚动到之前存储的值

                  What i did was..
                  in component5 I call onLayout in the main view and then save x and y in the parent component. To scroll to it in component 3 on click i call the parent function that uses the scrollview ref to scroll to the values stored before

                  组件5

                      export default class Component5 extends Component {
                  
                      saveLayout() {
                          this.view.measureInWindow((x, y, width, height) => {
                              this.props.callParentFunction(x, y)
                          })
                      }
                      render() {
                          return (
                              <View ref={ref => this.view = ref} onLayout={() => this.saveLayout()}>
                  
                              </View>
                          )
                      }
                  }
                  

                  组件3

                  export default class Component3 extends Component {
                  
                      render() {
                          return (
                              <View >
                                  <TouchableOpacity onPress={()=>{this.props.goToComponent5()}}>
                  
                                  </TouchableOpacity>
                              </View>
                          )
                      }
                  }
                  

                  家长:

                  export default class Parent extends Component {
                  constructor(props) {
                  this.goToComponent5=this.goToComponent5.bind(this)
                      super(props)
                      this.state = {
                          x:0,
                          y:0,
                      }
                  }
                  
                      callParentFunction(x, y) {
                          this.setState({ x, y })
                      }
                  
                      goToComponent5(){
                          this.ScrollView.scrollTo({x: this.state.x, y: this.state.y, animated: true});
                      }
                  
                      render() {
                          return (
                              <View >
                                  <ScrollView ref={ref => this.ScrollView = ref}>
                                      <Component1 />
                                      <Component2 />
                                      <Component3 goToComponent5={this.goToComponent5}/>
                                      <Component4 />
                                      <Component5 callParentFunction={this.callParentFunction}/>
                                  </ScrollView>
                              </View>
                          )
                      }
                  }
                  

                  这篇关于React Native ScrollView - 如何从另一个子组件按钮滚动到子组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                    <tbody id='go3AA'></tbody>

                  1. <tfoot id='go3AA'></tfoot>

                    <small id='go3AA'></small><noframes id='go3AA'>

                      <legend id='go3AA'><style id='go3AA'><dir id='go3AA'><q id='go3AA'></q></dir></style></legend>
                      • <i id='go3AA'><tr id='go3AA'><dt id='go3AA'><q id='go3AA'><span id='go3AA'><b id='go3AA'><form id='go3AA'><ins id='go3AA'></ins><ul id='go3AA'></ul><sub id='go3AA'></sub></form><legend id='go3AA'></legend><bdo id='go3AA'><pre id='go3AA'><center id='go3AA'></center></pre></bdo></b><th id='go3AA'></th></span></q></dt></tr></i><div id='go3AA'><tfoot id='go3AA'></tfoot><dl id='go3AA'><fieldset id='go3AA'></fieldset></dl></div>

                          <bdo id='go3AA'></bdo><ul id='go3AA'></ul>