【问题标题】:How to use swiper in react-native如何在 react-native 中使用 swiper
【发布时间】:2019-11-22 09:12:54
【问题描述】:

您好,我怎样才能使用来自Home 组件的数据在Profil 组件中工作。我在Home 组件中有一个状态,我在其中存储来自网络服务的数据。

class Accueil extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      refreshing: false,
      latitude:null,
      longitude:null,
      dataSource:[],
      error:null,
      appState: AppState.currentState,
      currentIndex: 0,
    }
    this.displayPosition = this.displayPosition.bind(this);
  }

getData(){
    fetch("SomeURL")
      .then(res => res.json())
      .then(result => {
        return(
        this.setState({
          dataSource: result
        }));
      })
      .catch(error => {
        console.error(error)
      })
  }

render() {
    return (
      <SafeAreaView style={{ flex:1 }}>
        <View style={styles.main_container}>
          <FlatList style={styles.flatList}
          data={this.state.dataSource}
          extraData = {this.state}
          keyExtractor={(item, index) => item.MembreId}
          renderItem={(item) => <UserItem user={item} displayDetailForUser={this._displayDetailForUser} displaySwipe = {this._displaySwipe}/>}
          numColumns={numColumns}
          refreshing={this.state.refreshing}
          onRefresh={this.handleRefresh} />
        </View>
      </SafeAreaView>
    )
  }
}

如何在Profil 组件中使用this.state.dataSource 以便.map 使用数据的刷卡者?

【问题讨论】:

  • 您在将值传递给配置文件组件时遇到问题吗??'
  • 是的,我无法在 Profil 组件中传递整个数据源
  • 但是,你必须声明你的 displayPosition 方法。
  • 不是我已经声明了displayPosition但是我没有在这里写,否则会太长

标签: react-native react-native-swiper


【解决方案1】:

要将值从一个组件传递到另一个组件,您可以使用以下代码 -

this.props.navigation.navigate('<Your Screen Name>', {
    dataSource: this.state.dataSource
});

现在您可以在您的组件中访问数据存储区,如下所示:

var tempDataSource = [...this.props.navigation.state.params.dataSource]

通过访问来使用它 this.props.navigation.state.params.dataSource 直接使用这个变量,或者你可以像上面一样将它保存在另一个变量中 与您的 swiper 一起使用。

希望对你有帮助,谢谢:)

【讨论】:

  • 我可以在 Home 组件的任何地方添加这个吗?
  • 个人资料和主页都是不同的屏幕,或者您在主屏幕内调用个人资料??
  • 它们是不同的屏幕。当我点击Home 组件中的配置文件时,它会将我发送到UserProfil
  • ok then....点击主屏幕上的,您必须提供我的第一个代码,然后在另一个屏幕上,即您必须访问的配置文件屏幕上数据源使用我的第二个代码@SauceCurry
  • 当我尝试控制台日志tempDataSource时得到Invalid attempt to spread non-iterable instance
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2018-08-02
  • 1970-01-01
相关资源
最近更新 更多