use the following code "import React, { useLayoutEffect, useState } from 'react'; import…
- User
use the following code "import React, { useLayoutEffect, useState } from 'react'; import { Image, Pressable, StyleSheet, Text } from 'react-native'; import Animated, { useSharedValue, withTiming, useAnimatedStyle, } from 'react-native-reanimated';
// Components import { LocationIcon } from '../components';
// Images const allLocationsIcon = require('@assets/home/header/allLocationsIcon.png');
type SimpleLocationItemProps = { color: string; label: string; index: number; onPress: (index: number) => void; };
const SimpleLocationItem = ({ color, label, index, onPress, }: SimpleLocationItemProps) => { const [isSeleted, setIsSelected] = useState(false);
useLayoutEffect(() => {}); const onItemPress = () => { setIsSelected(!isSeleted); onPress(index); };
return ( <Animated.View style={[styles.circleContainer]}> {label} </Animated.View> ); };
export default SimpleLocationItem;
const styles = StyleSheet.create({ circleContainer: { width: 48, height: 48, opacity: 1, }, character: { color: 'white', fontSize: 23, fontWeight: '600', }, }); " and modify it in a way that when the user taps the Pressable component, the Animated.View shows an animated orange border appear, is the user taps the control again the border disappears without animation
- Assistant
To achieve the desired effect, you can modify the code as follows:
import React, { useLayoutEffect, useState } from 'react'; import { Image, Pressable, StyleSheet, Text } from 'react-native'; import Animated, { useSharedValue, withTiming, useAnimatedStyle, } from 'react-native-reanimated'; // Components import { LocationIcon } from '../components'; // Images const allLocationsIcon = require('@assets/home/header/allLocationsIcon.png'); type SimpleLocationItemProps = { color: string; label: string; index: number; onPress: (index: number) => void; }; const SimpleLocationItem = ({ color, label, index, onPress, }: SimpleLocationItemProps) => { const [isSelected, setIsSelected] = useState(false); const borderColor = useSharedValue('transparent'); const animatedStyle = useAnimatedStyle(() => { return { borderColor: borderColor.value, borderWidth: isSelected ? withTiming(2) : withTiming(0), }; }); const onItemPress = () => { setIsSelected(!isSelected); borderColor.value = isSelected ? withTiming('transparent') : withTiming('orange'); onPress(index); }; return ( <Pressable onPress={onItemPress}> <Animated.View style={[styles.circleContainer, animatedStyle]}> <LocationIcon backgroundColor={color}> <Text style={styles.character}>{label}</Text> </LocationIcon> </Animated.View> </Pressable> ); }; export default SimpleLocationItem; const styles = StyleSheet.create({ circleContainer: { width: 48, height: 48, opacity: 1, borderRadius: 24, }, character: { color: 'white', fontSize: 23, fontWeight: '600', }, }); - User
Create a component that receives an array, the component renders a flatlist of a specific component type, when an item is press the item inside the flatlist will be notify to update its state
- Assistant
Sure! Here's an example of a component that receives an array and renders a FlatList with items that can update their state when pressed:
import React, { useState } from 'react'; import { FlatList, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; type Item = { id: number; label: string; } type CustomComponentProps = { data: Item[]; } const CustomComponent = ({ data }: CustomComponentProps) => { const [selectedItem, setSelectedItem] = useState<Item | null>(null); const renderItem = ({ item }: { item: Item }) => { const onPress = () => { setSelectedItem(item); }; const itemStyle = { backgroundColor: selectedItem === item ? 'orange' : 'white', }; return ( <TouchableOpacity onPress={onPress}> <View style={[styles.itemContainer, itemStyle]}> <Text style={styles.itemLabel}>{item.label}</Text> </View> </TouchableOpacity> ); }; return ( <FlatList data={data} renderItem={renderItem} keyExtractor={(item) => item.id.toString()} contentContainerStyle={styles.container} /> ); }; const styles = StyleSheet.create({ container: { paddingVertical: 16, paddingHorizontal: 8, }, itemContainer: { height: 48, justifyContent: 'center', paddingHorizontal: 16, marginBottom: 8, borderWidth: 1, borderColor: 'gray', borderRadius: 8, }, itemLabel: { fontSize: 16, }, }); export default CustomComponent; - User
Give me a working example in React native where I use react useContext to save values from 3 different screens and where I retrieve these values in a fourth screen.
- Assistant
- User
modify this code so I can call methods to set the value of individual properties : "import React, { createContext, useState, ReactNode } from 'react';
const ActivationContext = createContext(undefined); const ActivationDispatchContext = createContext(undefined);
type ActivationContextProps = { children: ReactNode; };
const ActivationProvider = ({ children }: ActivationContextProps) => { const [activationDetails, setActivationDetails] = useState({ planSku: '', });
return ( <ActivationContext.Provider value={activationDetails}> <ActivationDispatchContext.Provider value={setActivationDetails}> {children} </ActivationDispatchContext.Provider> </ActivationContext.Provider> ); };
export { ActivationProvider, ActivationContext, ActivationDispatchContext };"
- Assistant
- User
lets do another version where I will have all properties values inside of an object, its values will be modified by set methods that will receive a value, check if the object has it, if it has it, then updates its value, otherwise it adds the value to the object
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.