简体   繁体   English

React Native-为什么这个状态对象在功能上是空的?

[英]React Native - Why is this state object empty in function?

I have this React-Native component, which renders an image gallery, in a list. 我有这个React-Native组件,它在列表中呈现一个图像库。 So it's invoked once per each item on that parent list. 因此,该父列表中的每个项目都会被调用一次。 It receives two props, "etiqueta" which has the title of the item, and "galleries", which is an array of galleries (javascript literals). 它接收两个道具,一个是带有标题的“ etiqueta”,另一个是画廊(javascript文字)数组“ galleries”。 I know it should only receive the gallery that we need to render, but I couldn't do it, so for now, I'm sending all the galleries and filtering them in this component. 我知道它应该只接收我们需要渲染的画廊,但是我做不到,所以现在,我将发送所有画廊并将其过滤到此组件中。

Problem: when I console.log the state in ComponentDidMount everything looks good, state.entries has the gallery that need to be rendered. 问题:当我console.log在ComponentDidMount中的状态看起来一切正常时,state.entries具有需要呈现的库。 But when I try to access it in get Example2() it returns an empty array. 但是,当我尝试在get Example2()中访问它时,它将返回一个空数组。 I'm not sure why this is happening. 我不确定为什么会这样。

And also, for some reason, the console.log in get Example2() keeps running again and again in the console. 而且,由于某种原因, get Example2()中的console.log也在控制台中一次又一次地运行。 Why is this happening? 为什么会这样呢?

This is the gallery I'm trying to use: https://github.com/archriss/react-native-snap-carousel 这是我要使用的画廊: https : //github.com/archriss/react-native-snap-carousel

Thank you for your time of course, I hope I'm clear enough, I'm new to React. 当然,感谢您的时间,我希望我足够清楚,我是React的新手。

class EtiquetaDetail extends Component {

  constructor(props) {
    super(props);

    this.state = {
      slider1ActiveSlide: 0,
      slider1Ref: null,
      entries: [],
      isMounted: false
    };
  }

  componentDidMount() {

    let etiqueta_id = this.props.etiqueta.id ;

    if ( this.props.etiqueta ) {

      // select the gallery we need to render
      gallery = this.props.galleries.find(function (obj) {
        return obj.id_gal === etiqueta_id;
      });

      ENTRIES1 = JSON.parse( gallery.data );

      this.setState = {
        slider1ActiveSlide: 0,
        slider1Ref: null,
        entries: ENTRIES1,
        isMounted: true
      };
      console.log(this.state); // this outputs everything as expected

    }

  }

get example2 () {
  console.log(this.state.entries); // returns 0 
    return (
        <View>
            <Carousel
              data={ this.state.entries }
            />
        </View>
    );
}

  render() {

    const etiqueta = this.props;

    const { title } = this.props.etiqueta;

    return (
      <Card>
        <CardSection>
          <View>
            <Text>{title.rendered}</Text>
          </View>
        </CardSection>

        <SafeAreaView>
            <View}>
                <ScrollView>
                    { this.example2 }
                </ScrollView>
            </View>
        </SafeAreaView>

      </Card>
    )
  };

};

When you need to update the state outside of the constructor, use this.setState , to let React know it needs to do a re-render. 当您需要在构造函数之外更新状态时,请使用this.setState ,以让React知道它需要进行重新渲染。

this.setState({
  slider1ActiveSlide: 0,
  slider1Ref: null,
  entries: ENTRIES1,
  isMounted: true
})

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

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