When I use <ExpandableView><Text>hi</text></ExpandableView> tags like this I want the…
- User
When I use hi tags like this I want the text to go inside the expanded content
const ExpandableView = ({ expanded = false }) => { const [height] = useState(new Animated.Value(0)); useEffect(() => { Animated.timing(height, { toValue: expanded ? 200 : 0, duration: 700, easing: Easing.out(Easing.exp), useNativeDriver: false }).start(); }, [expanded, height]); return ( <Animated.View style={{ height }}> {/* {children go here} */} </Animated.View> ); };
- Assistant
To ensure that the children inside
<ExpandableView>render within the animated container, you need to pass them as props and render them inside the animated view. Here’s how you can update yourExpandableViewcomponent:- Accept
childrenas a prop. - Render
childreninside the animated container.
Here's your updated code:
import React, { useState, useEffect } from 'react'; import { Animated, Easing, Text } from 'react-native'; const ExpandableView = ({ expanded = false, children }) => { const [height] = useState(new Animated.Value(0)); useEffect(() => { Animated.timing(height, { toValue: expanded ? 200 : 0, duration: 700, easing: Easing.out(Easing.exp), useNativeDriver: false }).start(); }, [expanded, height]); return ( <Animated.View style={{ height, overflow: 'hidden' }}> {children} </Animated.View> ); }; const YourComponent = () => { const [isExpanded, setIsExpanded] = useState(false); return ( <ExpandableView expanded={isExpanded}> <Text>hi</Text> </ExpandableView> ); }; export default YourComponent; - Accept