Изменение текста React Native Flatlist - PullRequest
0 голосов
/ 20 июня 2020

Я меняю свой код, связанный с ответом.

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>
    )
}

Я считаю, что при прикосновении к любому элементу списка данных (он поступает из массива данных) он изменит текст, связанный со значением массива data2 с тот же идентификатор

John Doe1

John Doe2

John Doe3 <- touch then </p>

John Doe1

John Doe2

Sample2 Doe3 <- Заменить текст на этот </p>

Как эта строка с изменением текста

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 ]

1 голос
/ 21 июня 2020

у вас есть два набора данных, поэтому вам нужно отфильтровать данные, проверьте, что этот пример может работать для вас https://snack.expo.io/@jsfit / 223f2 c

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>
  );
};

0 голосов
/ 21 июня 2020
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;

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...