【问题标题】:How to divide array in FlatList and Text in react native如何在反应原生中划分FlatList和Text中的数组
【发布时间】:2019-10-29 06:15:27
【问题描述】:

如何放置数组的第一行

"id":"1","name":"Some Name","bal":"1000"

<Text>{name} {balance}</Text> 然后是<FlatList /> 中的其他 2 行。我可以通过获取数组来显示 FlatList 或 Text,但不能同时显示两者。如果我让另一个屏幕放入第一行然后将其导入第二个屏幕,我就能够显示所需的结果。但是我认为有更好的方法可以将这个数组划分为单个屏幕并显示Text和FlatList。谢谢。

[
  [
    {
      "id": "1",
      "name": "Some Name",
      "bal": "1000",
      "ab": "1000"
    }
  ],
  {
    "area": "North",
    "building": "Some Building",
    "unit_number": "123"
  },
  {
    "area": "North",
    "building": "Some Building",
    "unit_number": "123"
  }
]

更新:请查看代码。我已经注释掉了我需要在

中提取和显示数组的第一行的行
export default class ClientDetails extends Component {


    constructor() {
        super()
        this.state = {
           isLoading: true
        }
    }
    renderItem = ({ item }) => {
        return (
     <View>
    <Text>
       {item.area}
       </Text>
       <Text>
        Rent Amount:  {item.building}
        </Text>
       <Text>
        Rent Amount:  {item.unit_number}
        </Text>
    </View>
        )
      }
    componentDidMount = () => {
    return fetch("http://192.168.0.106/db_1/list.php?id=1")
                .then((response) => response.json())
                .then((responseJson) => {
                this.setState({
                    isLoading: false,
                    dataSource: responseJson,
                })
            })
    }
    render() {
return (
       <View style={styles.MainContainer}>

//Here I was to put <Text> {id}{name}{bal}</Text> but I can either get <Text> to display or <FlatList>
  <FlatList
            data={ this.state.dataSource }
               ItemSeparatorComponent = {this.renderSeparator}
     renderItem={this.renderItem}
    keyExtractor={(item, index) => index.toString()}
/>
 </View>

        );
    }
}

【问题讨论】:

  • 我无法理解您的意图,您能否描述清楚并提供示例图片
  • 你在问题​​中写的数组无效?
  • 正如@VigneshVPai 所说,您的数组形状不合适。
  • @in.k,但我可以让 显示或 ,这是什么意思?

标签: arrays react-native react-native-flatlist


【解决方案1】:

您可以在 renderItem 中过滤掉 FlatList 的客户信息。在您的情况下,这可能有效:

renderItem = ({ item }) => {
  if (item.area != null){
    return (
      <View>
        <Text>
        {item.area}
        </Text>
        <Text>
         Rent Amount:  {item.building}
         </Text>
        <Text>
         Rent Amount:  {item.unit_number}
         </Text>
     </View>
    )
  } 
}

render() {
  const data = this.state.dataSource

  return (
    <View style={styles.container}>
      { data && data[0] && data[0][0] ?
      <Text>{data[0][0].id} {data[0][0].name} {data[0][0].bal}</Text> : null }
      <FlatList
        data={ data }
        ItemSeparatorComponent = {this.renderSeparator}
        renderItem={this.renderItem}
        keyExtractor={(item, index) => index.toString()}/>
    </View>
  );
}

【讨论】:

  • 但是数据是通过url动态获取的。我如何定义使用您建议的 const 数据来动态获取数据?谢谢
  • 动态地好像数组可以为空?如果可能,您能否列出可能的数据源(例如最佳/最坏情况,哪些数据是固定的),以便我可以尝试提供更好的解决方案。
  • No 数据不能为空。我的意思是在您提到 const data = [] 的代码中。那是您手动输入的数据。我的意思是,一旦数据被获取,正如您在 url 的原始帖子中看到的那样并且有响应,我如何将该数据放入您的 const data
  • 我明白了。根据您的原始帖子,您可以在ComponentDidMount 中将responseJson 中的this.state.dataSource 替换为this.state.dataSource。让我更新我的答案。
  • 获取错误未定义不是数据[0]的对象。如果我删除此行,则平面列表输出正常。
【解决方案2】:

您可以将静态数据放在该特定数组的第一项中。否则,如果您想要相同的静态数据,请使用 SectionList 并使用 renderSectionHeader 道具来使用静态元素。

【讨论】:

  • 感谢您的评论。我不想要 if else 等。我想要 项目。 FlatList 显示属性和 Text 显示客户端详细信息
  • 使用 SectionList 和标题项我想你会得到你的东西
  • 正如我在帖子中提到的,如果我使用 dataSource: responseJson,或者如果我将其更改为例如,我可以获得 FlatList。 cn: responseJson.name, balance: responseJson.bal 然后我得到文本。
  • 我不想要标题我需要在屏幕上显示包装在视图和文本中的客户详细信息,因为可以为客户提供更多详细信息,而不仅仅是余额和名称
【解决方案3】:

你可以将数据保存在不同的 state props 中,而不是放在 props 中。我希望下面的代码可以匹配你的问题

constructor(props){
  this.state = {
     isLoad:false,
     listData:[],
     idInfo:{}
  }
}

render(){
<View>
<this.state.idInfo && this.state.dataList.length == 0 && <Text>{this.state.idInfo.name}</Text>
<this.state.dataList.lenght>0 && <FlatList/>
<View>
}

componentDidMount = () => {
    // I suggest you request in different methods, the according to the 
   //return data, update the state 
    return fetch("http://192.168.0.106/db_1/list.php?id=1")
                .then((response) => response.json())
                .then((responseJson) => {
                if("responseJson is list"){
                this.setState({
                    isLoading: false,
                    listData: responseJson,
                })}
               if("respnseJson is the id you say"){
                  this.setState({
                    isLoading: false,
                    idInfo: responseJson,
                })}
               }
            })
    }

【讨论】:

  • 感谢您的代码。查看您的代码只需考虑没有 if else 语句。我想退回他们两个。首先,我想返回具有 id、名称、余额的 ,然后我想返回具有客户端属性列表的
  • item 是按索引渲染的,如果要先显示数组的第一个元素,然后显示其余元素,但第一个元素视图与其余元素不同,它可以满足您的要求
  • @ in.k ,我知道你想要每次当项目是{name,..}时,然后渲染文本。我认为你首先必须拆分与 secionlist 数据匹配的数组,然后使用部分列表
  • 我尝试调整您的代码并删除 if else 但问题仍未解决。我认为现在的设置对我来说是怎样的,我将保留它,它正在从另一个屏幕导入第一部分。唯一的问题是,如果有不同的数据为同一个客户端获取不同的信息,它将继续为每个数据添加新屏幕。无论如何,非常感谢您的帮助
  • @in.k,我更新了答案,如果你想根据不同的项目数据来渲染项目。如果你想根据不同的sourceData,你可以使用&lt; "some condition" &amp;&amp; &lt;View&gt;"some view you want to render"&lt;/View&gt;
猜你喜欢
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
  • 2018-05-11
  • 2021-08-04
  • 2022-01-02
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多