【问题标题】:Lodash _.reduce not recognized in React NativeLodash _.reduce 在 React Native 中无法识别
【发布时间】:2018-03-12 20:35:05
【问题描述】:

我不确定这是否是我的错误,因为我找不到我使用的代码与文档和视频中的代码之间的任何区别。

我有一段代码使用SectionList来显示使用lodash的groupByreduce方法排列并打包到数组中的数据。

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  ScrollView,
  Button,
  Picker,
  SectionList
} from 'react-native';

import MapView from 'react-native-maps';
import MapViewDirections from 'react-native-maps-directions';
import { SearchBar } from 'react-native-elements';
import _ from 'lodash'; 



type Props = {};

export default class App extends Component<Props> {

  constructor(props) {
    super(props);
    this.findNearestBranch = this.findNearestBranch.bind(this);
    this.displayDirections = this.displayDirections.bind(this);
    this.displayUserLocationPin = this.displayUserLocationPin.bind(this);
    this.calculateNearestBranch = this.calculateNearestBranch.bind(this);
  }

  state = {
   ...
    branchArray: [
      {branchId: 12, name: "Main SSNIT Branch", description: "Where the big boys work", latitude:  5.559971399999999, longitude:-0.2002176999999392, region: "Northern Region" },
      {branchIdid: 52, name: "Aother smaller branch", description: "Where the small boys work", latitude:  6.559971399999999, longitude:-0.2002176999999392, region: "Eastern Region" }, 
      {branchId: 78, name: "Yet another random branch", description: "Where the crazy girls work", latitude:  7.059971399999999, longitude:-0.2002176999999392, region: "Northern Region" }, 
      {branchId: 96, name: "This isn't a branch. It's a twig", description: "Where the strange people play", latitude:  6.859971399999999, longitude:-0.2002176999999392, region: "Western Region" }
     ]
  }

  render() {

    return (
      <ScrollView contentContainerStyle={styles.container} >   

     ...


      <SectionList
        renderItem={this.renderBranchItem}
        renderSectionHeader={this.renderBranchHeader}
        sections={this.getBranchesForList}
        keyExtractor = {(item) => item.branchId}
      />

      </ScrollView>

    );
  }

  renderBranchItem = (branchItem) =>{
    return <View style={{borderBottomWidth:StyleSheet.hairlineWidth}}>
            <Text style={{fontSize:20, fontWeight:"bold"}}> {branchItem.item.name} </Text>
            <Text style={{fontSize:15,}}> {branchItem.item.description} </Text>
            <Text style={{fontSize:15,}}> {branchItem.item.longitude}, {branchItem.item.latitude}</Text>
            </View>
  }

  renderBranchHeader = (header) =>{
    return <View style={{borderBottomWidth:StyleSheet.hairlineWidth}}>
            <Header style={{fontWeight:"bold"}} title={header.section.region} />
            </View>
  }

  getBranchesForList = () =>{

    //Making an object containing the branches sorted by region {region: [branch1, branch2], region2: [branch 2, branch 4]}
    let sortedBranchesObject = _.groupBy(this.state.branchArray, branch => branch.region);

    //Now making that Object an array of structure [{data: [], region: "region"}, {...}]
    let letsortedBranchesArray = _.reduce(sortedBranchesObject, (accumulatingArray, nextValue, nextKey)=>{
      accumulatingArray.push({
        data: nextValue,
        region: nextKey
      });
      return accumulatingArray;
    }, []);

    return sortedBranchesArray;
  }

}



const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  map: {
    height:400, 
    width: 400, 

  }
});

但是,由于某种原因,_.reduce 在getBranchesForList 中未被识别为函数。这是错误日志的图片:

我在这里做错了吗?老实说,我不确定。我发布了一个 GitHub issue 关于它,但我得到的唯一答案是我对 _ 的引用可能已经改变。不过,我找不到任何证据表明这种事情正在发生。

【问题讨论】:

  • 只是一个注释。您不需要执行this.findNearestBranch = this.findNearestBranch.bind(this);,因为您使用的是箭头函数和类属性。实际上,您可以删除整个constructor,截至目前为止,它没有任何用处。
  • 哦,这是为了提醒!我会清理它

标签: javascript react-native lodash


【解决方案1】:

您的问题与lodash.reduce 无关。这是关于您的 sections 不是 SectionList 期望的数组。

部分数组

Docs.

<SectionList
    renderItem={this.renderBranchItem}
    renderSectionHeader={this.renderBranchHeader}
    sections={this.getBranchesForList()} // NB call
    keyExtractor = {(item) => item.branchId}
  />

【讨论】:

    猜你喜欢
    • 2022-12-19
    • 2016-10-03
    • 2021-04-27
    • 1970-01-01
    • 2016-07-07
    • 2020-10-20
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    相关资源
    最近更新 更多