【问题标题】:how to effectively store data in react native snap carousel and access them inside carousel using data & renderItem如何在反应原生快照轮播中有效地存储数据并使用数据和渲染项在轮播中访问它们
【发布时间】:2017-09-05 03:35:13
【问题描述】:

我想将数据作为数组存储在我的轮播 js 文件中,并使用 data={this.state.data} renderItem={this.renderItem} 访问 <Carousel> 中的这些数据

我该怎么做

我的代码中有 8 张不同的图像作为数据

这就是我现在的代码:

import React, { Component } from 'react';
import { Dimensions, View, Image } from 'react-native';
import Carousel from 'react-native-snap-carousel';

const { height, width } = Dimensions.get('window');

class TeamScroll extends Component {

  render() {
    return (
      <View >
      <View style={{ transform: [{ rotate: '-14deg' }] }}>
      <Carousel
      inactiveSlideOpacity={0.6}
      inactiveSlideScale={0.65}
      firstItem={1}
      sliderWidth={width}
      itemWidth={width / 3} >
      <Image
      source={require('./Images/logo-chepauk.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logo-dindigul.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logo-kanchi.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logo-karaikudi.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logo-kovai.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logomadurai.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logothiruvallur.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logotuti.png')}
      style={styles.logoStyle}  />
      </Carousel>
      </View>
      </View>
);
}
}
const styles = {
  logoStyle: {
    transform: [{ rotate: '14deg' }],
    width: width / 3,
    height: width / 3
    }
};
export default TeamScroll;

我想在里面使用data={this.state.data} renderItem={this.renderItem}并访问这些图像数据

我还想将这些图像存储在一个数组中并使用它们

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:
    import React, { Component } from 'react';
    import { Dimensions, View, Image } from 'react-native';
    import Carousel from 'react-native-snap-carousel';
    
    const { height, width } = Dimensions.get('window');
    
    const images = [
      require('./Images/logo-chepauk.png'),
      require('./Images/logo-dindigul.png'),
      require('./Images/logo-kanchi.png'),
      require('./Images/logo-karaikudi.png'),
      require('./Images/logo-kovai.png'),
      require('./Images/logo-logomadurai.png'),
      require('./Images/logo-logothiruvallur.png'),
      require('./Images/logo-logotuti.png'),
    ];
    
    class TeamScroll extends Component {
      renderItem = ({ item }) => {
        return (
          <Image
            source={item}
            style={styles.logoStyle}
          />
        );
      }
    
      render() {
        return (
          <View>
            <View style={{ transform: [{ rotate: '-14deg' }] }}>
              <Carousel
                inactiveSlideOpacity={0.6}
                inactiveSlideScale={0.65}
                firstItem={1}
                sliderWidth={width}
                itemWidth={width / 3}
                data={images}
                renderItem={this.renderItem}
              />
            </View>
          </View>
        );
      }
    }
    
    const styles = {
      logoStyle: {
        transform: [{ rotate: '14deg' }],
        width: width / 3,
        height: width / 3
        }
    };
    
    export default TeamScroll;
    

    试试这个,我没测试过。

    【讨论】:

    • 谢谢,这就是我所需要的,但是我们可以为数组项添加一个名称以便在其他 js 文件中使用它们
    • 但项目未显示
    【解决方案2】:

    如果您尝试创建可重用数组,您可以创建另一个名为 images-file.js 的文件并

    import logo-chipauk from './Images/logo-chepauk.png'
    ... add other images the same way ...
    import logo-logotuti from './Images/logo-logotuti.png'
    
    export default images = [
       logo-chipauk,
       ... other images,
       logo-logotuti
    ];
    

    然后,在任何其他文件中,

    import images from 'image-file.js'
    

    【讨论】:

    • 但是这个 import logo-chepauk from './Images/logo-chepauk.png'; 在 eslint 中产生了意外的标记
    • 它引用的是什么令牌?
    • 在第一行,即import logo-chepauk from './Images/logo-chepauk.png'; -chepauk 是它所指的不是路径中的chepauk 之后的另一行
    • 是的。该行等于var logoChepauk = require('./Images/logo-chepauk.png'); 您只是将来自.png 的内容分配给一个名称。
    • 我知道您只是想回答您的问题,但有时您必须查一下这些资料。回答:当我匆忙输入那个答案时,我不小心命名了默认导出。从默认导出中删除名称。资源:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    猜你喜欢
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2019-09-25
    • 2021-09-20
    • 2016-02-22
    • 2017-10-26
    相关资源
    最近更新 更多