简体   繁体   English

React Native和Refs-为什么此函数不返回我的内容?

[英]React Native and Refs - Why does this function not return my content?

I have the following code in react native: 我在React Native中有以下代码:

'use strict'

import React, { Component } from 'react';
import { AppRegistry, Animated, Dimensions, Easing, Image, Text, View, StyleSheet, TouchableOpacity, ScrollView } from 'react-native';

const categoryTwoLinks = [
  { label: 'Subcategory1' },
  { label: 'Subcategory2' },
  { label: 'Subcategory3' },
  { label: 'Subcategory4' },
  { label: 'Subcategory5' },
  { label: 'Subcategory6' },
]

const categoryTouts = [
  { image: require('./assets/images/image01.jpg'), title: 'Category1' },
  { image: require('./assets/images/image02.jpg'), title: 'Category2', links: categoryTwoLinks },
  { image: require('./assets/images/image03.jpg'), title: 'Category3' },
]

class Tout extends Component {
  render() {
    console.log('tout')
    return (
      <Image
        source={this.props.image}
        ratioGrow
        alignItems='center'
        style={{ alignItems: 'center', justifyContent: 'center' }}
      >
        <Text
          style={{ color: 'white', fontSize: 24, backgroundColor: 'transparent', fontWeight: 'bold' }}
        >
          {this.props.title}
        </Text>
      </Image>
    )
  }
}

export default class scrollAccordion extends Component {

  handleScroll = ({ nativeEvent: { contentOffset } }: Object) => {
    const scrollY = contentOffset.y
    this.scrollOffsetValue.setValue(scrollY)
  }

  setScrollRef = node => {
    console.log('set scroll ref')
    if (node) {
      this.scrollRef = node
    }
  }

  renderCategoryTouts = () => {
    console.log('render category tout')
    categoryTouts.map((tout, index) => {
      return (
        <Tout
          key={tout.title}
          {...tout}
          scrollViewRef={this.state.scrollRef}
          scrollOffset={this.scrollOffsetValue}
        />
      )
    })
  }

  render() {
    console.log('render')
    let categoryTouts = this.scrollRef ? this.renderCategoryTouts() : null
    return (
      <View style={styles.container}>
        <ScrollView
          scrollEventThrottle={1}
          ref={this.setScrollRef}
          onScroll={this.handleScroll}
        >
          <View>
            {categoryTouts}
          </View>
        </ScrollView>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: 40,
    backgroundColor: 'white',
  },
  toutText: {
    color: 'white', backgroundColor: 'transparent', fontWeight: 'bold', fontSize: 24
  }
});

AppRegistry.registerComponent('scrollAccordion', () => scrollAccordion);

which is not returning any content from the renderCategoryTouts function. 不会从renderCategoryTouts函数返回任何内容。

I know that initially, this.scrollRef is undefined, so the main render function will return {null} for the {categoryTouts} variable. 我知道,最初, this.scrollRef是未定义的,因此主渲染函数将为{categoryTouts}变量返回{null} But after the scrollRef is set, I need it to return the results of renderCategoryTouts . 但是在设置scrollRef之后,我需要它返回renderCategoryTouts的结果。 How do I get this to work? 我该如何工作?

In renderCategoryTouts function of yours you have a variable categoryTouts variabke. 在您的renderCategoryTouts函数中,您具有变量categoryTouts变量。 You have never declared a variable like that in this function so I assume it is a global variable. 您从未在此函数中声明过像这样的变量,因此我认为它是全局变量。

 renderCategoryTouts = () => {
    categoryTouts.map((tout, index) => {
      return (
        <Tout
          key={tout.title}
          {...tout}
          scrollViewRef={this.state.scrollRef}
          scrollOffset={this.scrollOffsetValue}
        />
      )
    })
  }

And in this function you're using this.state.scrollRef, this.scrollOffsetValue so you need to bind this function.(I suggest you do that in the constructor of class) 在此函数中,您使用this.state.scrollRef,this.scrollOffsetValue,因此您需要绑定此函数。(建议您在类的构造函数中进行此操作)

this.renderCategoryTouts = this.renderCategoryTouts.bind(this) this.renderCategoryTouts = this.renderCategoryTouts.bind(this)

also in case this does not work full code would be very helpful 另外,如果这不能正常工作,那么完整的代码将非常有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM