反应本机Flatlist文本更改

时间:2020-06-20 17:50:07

标签: react-native react-native-flatlist

我更改了与答案相关的代码。

import React, { useState, } from "react";
import { View, Text, FlatList, SafeAreaView, TouchableOpacity, } from "react-native";


const ToggleButton = (props) => {
    const {
        sample,
        id,
        onPress,
    } = props;
    const [isPressed, setIsPressed] = useState(false);
    const text = isPressed ? `${sample}-${id}` : sample;

    return (

        <TouchableOpacity onPress={() => {
            setIsPressed(!isPressed);
            onPress && onPress();
        }}
        style={{ flex: 1, }}>        
            <View style={{ flex: 1, width: "100%", height: 100, borderWidth: 1, justifyContent: "center", alignItems: "center", }}>
                <Text style={{ fontSize: 20 }}>{ text }</Text>
            </View>
        </TouchableOpacity>
    )
};

const ToggleExample =() => {
    const data = [
        { sample:"John Doe1", id:"1" },
        { sample:"John Doe2", id:"2" },
        { sample:"John Doe3", id:"3" },
        { sample:"John Doe4", id:"4" },
        { sample:"John Doe5", id:"5" },  
    ];
    const data2 = [
      { sample2:"Sample2 Doe1", id:"1" },
      { sample2:"Sample2 Doe2", id:"2" },
      { sample2:"Sample2 Doe3", id:"3" },
      { sample2:"Sample2 Doe4", id:"4" },
      { sample2:"Sample2 Doe5", id:"5" },  
  ];

    return (
        <SafeAreaView style={{ flex: 1 }}>
            <FlatList
            data={data}
            renderItem={entry => {
                const { item } = entry;
                return (<ToggleButton {...item} />);
            }}
            contentContainerStyle={{ padding: 20, }}
            ItemSeparatorComponent={() => { return (<View style={{ flex: 1, height: 10, }}/>) }}
            keyExtractor={(entry, index) => index.toString()}
            />
        </SafeAreaView>
    )
}

我的事情是,当触摸任何列表项数据(来自数据数组)时,它将更改与具有相同id的data2数组值相关的文本

John Doe1

John Doe2

John Doe3 <-然后触摸

John Doe1

John Doe2

Sample2 Doe3 <-将文本更改为此

喜欢此文本更改行

const text = isPressed ? `${sample}-${id}` : sample;

be like
const text = isPressed ? `${sample2}-${id}` : sample;

如果不可能的话,这种类型就足够了

  const data = [
    { sample: 'John Doe', id: '1' },
    { sample: 'John Doe', id: '2' },
    { sample: 'John Doe', id: '3' },
    { sample: 'John Doe', id: '4' },
    { sample: 'John Doe', id: '5' },
    { sample: 'Samplee2 Doe1', id: '6' },
    { sample: 'Samplee2 Doe2', id: '7' },
    { sample: 'Samplee2 Doe3', id: '8' },
    { sample: 'Samplee2 Doe4', id: '9' },
    { sample: 'Samplee2 Doe5', id: '10' }
  ];
  const text = isPressed ? `${sample}-${id+5}` : sample;

感谢您的关注和事先的努力

2 个答案:

答案 0 :(得分:1)

您有两个数据集,因此需要过滤数据 检查此示例可能对您有用https://snack.expo.io/@jsfit/223f2c

import React, { useState } from 'react';
import {
  View,
  Text,
  FlatList,
  SafeAreaView,
  TouchableOpacity,
} from 'react-native';

const ToggleButton = (props) => {
  const [isPressed, setIsPressed] = useState(false);
  const { sample, id, onPress, item1, item2 } = props;
  const text = isPressed ? item2.sample2 : item1.sample;

  return (
    <TouchableOpacity
      onPress={() => {
        setIsPressed(!isPressed);
        onPress && onPress();
      }}
      style={{ flex: 1 }}>
      <View
        style={{
          flex: 1,
          width: '100%',
          height: 100,
          borderWidth: 1,
          justifyContent: 'center',
          alignItems: 'center',
        }}>
        <Text style={{ fontSize: 20 }}>{text}</Text>
      </View>
    </TouchableOpacity>
  );
};

const ToggleExample = () => {
  const data = [
    { sample: 'John Doe1', id: '1' },
    { sample: 'John Doe2', id: '2' },
    { sample: 'John Doe3', id: '3' },
    { sample: 'John Doe4', id: '4' },
    { sample: 'John Doe5', id: '5' },
  ];
  const data2 = [
    { sample2: 'Sample2 Doe1', id: '1' },
    { sample2: 'Sample2 Doe2', id: '2' },
    { sample2: 'Sample2 Doe3', id: '3' },
    { sample2: 'Sample2 Doe4', id: '4' },
    { sample2: 'Sample2 Doe5', id: '5' },
  ];

  return (
    <SafeAreaView style={{ flex: 1 }}>
      <FlatList
        data={data}
        renderItem={(entry) => {
          const { item } = entry;
          return (
            <ToggleButton
              item1={item}
              item2={data2.filter((_item) => _item.id === item.id)[0]}
            />
          );
        }}
        contentContainerStyle={{ padding: 20 }}
        ItemSeparatorComponent={() => {
          return <View style={{ flex: 1, height: 10 }} />;
        }}
        keyExtractor={(entry, index) => index.toString()}
      />
    </SafeAreaView>
  );
};

答案 1 :(得分:0)

import React, { useState, } from "react";
import { View, Text, FlatList, SafeAreaView, TouchableOpacity, } from "react-native";


const ToggleButton = (props) => {
    const {
        sample,
        id,
        onPress,
    } = props;
    const [isPressed, setIsPressed] = useState(false);
    const text = isPressed ? `${sample}-${id}` : sample;

    return (

        <TouchableOpacity onPress={() => {
            setIsPressed(!isPressed);
            onPress && onPress();
        }}
        style={{ flex: 1, }}>        
            <View style={{ flex: 1, width: "100%", height: 100, borderWidth: 1, justifyContent: "center", alignItems: "center", }}>
                <Text style={{ fontSize: 20 }}>{ text }</Text>
            </View>
        </TouchableOpacity>
    )
};

const ToggleExample =() => {
    const data = [
        { sample:"John Doe", id:"1" },
        { sample:"John Doe", id:"2" },
        { sample:"John Doe", id:"3" },
        { sample:"John Doe", id:"4" },
        { sample:"John Doe", id:"5" },  
    ];

    return (
        <SafeAreaView style={{ flex: 1 }}>
            <FlatList
            data={data}
            renderItem={entry => {
                const { item } = entry;
                return (<ToggleButton {...item} />);
            }}
            contentContainerStyle={{ padding: 20, }}
            ItemSeparatorComponent={() => { return (<View style={{ flex: 1, height: 10, }}/>) }}
            keyExtractor={(entry, index) => index.toString()}
            />
        </SafeAreaView>
    )
}

export default ToggleExample;

enter image description here