Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

javascript - React Native/TypeScript - How to manage state in functional component and apply flexible style with FlatList

Can anyone tell me how to solve these things?
1.if the list is pressed, I'd like to change the background color of the list beige(#FFF5E7) to white(#FBFBFB)
2.Also, I'd like to change the read value of the Object fales to true with useState

Problem is that if I pressed the list, whole background color of the list will be changed.

index.tsx

import React, { useState } from 'react';
import { Text, View, TouchableOpacity, FlatList } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import { DETAIL } from '../../sample';
import { Object } from './sample';

export default function sample() {
  const [state, setState] = useState(Object);
  const navigation = useNavigation();
  let Element;

  const renderItem = ({ item }) => {
    const readState = () => {
      navigation.navigate(NOTICE_DETAIL);
      const readValue = [...state];
      let value = { ...readValue[0] };
      value.read = true;
      readValue[0] = value;
      setState(readValue);
    };
    if (state[0].read) {
      Element = (
        <TouchableOpacity onPress={readState}>
          <View style={[styles.row, { backgroundColor: '#FBFBFB' }]}>
            <View style={styles.container}>
              <View style={styles.end}>
                <Text style={styles.text}>{item.text}</Text>
                <Text style={styles.time}>{item.time}</Text>
              </View>
              <Text style={styles.content}>{item.content}</Text>
            </View>
          </View>
        </TouchableOpacity>
      );
    } else {
      Element = (
        <TouchableOpacity onPress={readState}>
          <View style={[styles.row, { backgroundColor: '#FFF5E7' }]}>
            <View style={styles.container}>
              <View style={styles.end}>
                <Text style={styles.text}>{item.text}</Text>
                <Text style={styles.time}>{item.time}</Text>
              </View>
              <Text style={styles.content}>{item.content}</Text>
            </View>
          </View>
        </TouchableOpacity>
      );
    }
    return Element;
  };
  }

  return (
    <View style={{ flex: 1 }}>
      <FlatList
        extraData={Object}
        data={Object}
        renderItem={renderItem}
      />
    </View>
  );

}

Object.ts

export const Object = [
 {
  id: 1,
  text: 'testtesttest',
  content: 'testtesttest'
  read: false
},
{ 
  id: 2,
  text: 'testtesttest',
  content: 'testtesttest'
  read: false
}
  id: 3,
  text: 'testtesttest',
  content: 'testtesttest'
  read: false
}
]
question from:https://stackoverflow.com/questions/66059939/react-native-typescript-how-to-manage-state-in-functional-component-and-apply

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We can add inline styles into the component style props along with static styles,

For Example:

<TouchableOpacity onPress={readState}>
    <View style={[styles.row, { backgroundColor: item.read ? '#FBFBFB' : 'FFF5E7' }]}>
        <View style={styles.container}>
          <View style={styles.end}>
            <Text style={styles.text}>{item.text}</Text>
            <Text style={styles.time}>{item.time}</Text>
          </View>
          <Text style={styles.content}>{item.content}</Text>
       </View>
    </View>
</TouchableOpacity>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...