Home > Enterprise >  How to use hooks as image's source in react native?
How to use hooks as image's source in react native?

Time:01-06

Im making this menu that when the user clicks at an option, it changes the background image, but i cant use the hook that i created as a parameter to the source of the image. Can someone find where im wrong and how to fix it? Heres the part of my code referents to the menu and the hooks:

export function Home(){
    let imagens = {
        vovo: '../assets/vovoJuju.png',
        mc: '../assets/mcJuju.png',
        pato: '../assets/patoJuju.png',
    }
    const navigation = useNavigation<any>();
    const [showBottomSheet, setShowBottomSheet] = React.useState(false);
    const [param, setParam] = useState(1);
    const [skin, setSkin] = useState('vovo')
     const hide = () => {
        setShowBottomSheet(false)
    }
    function handleAbout(){
        navigation.navigate('About');
    }
    
    useEffect(() => {
        if(param==1){
            setSkin('vovo');
        }
        else if(param==2){
            setSkin('mc');
        }
        else if(param==3){
            setSkin('pato');
        }
    })
 
    return(
        <SafeAreaView style={styles.container}>
            <TouchableOpacity onPress={handleAbout}>
                <Counter />
            </TouchableOpacity>
            
            <TouchableOpacity onPress={() => {
            setShowBottomSheet(true)
          }}
            >
                <Image source={skin} style={styles.imgvj}/>
            </TouchableOpacity>

            <BottomSheet show={showBottomSheet} height={290} onOuterClick={hide}>
                <Pressable onPress={hide} style={styles.bottomSheetContent}>
                    <Image source={barrinhaLoja} style={styles.barra}/>
                    </Pressable>
                <View style={styles.conteudoLoja}>
                    <View style={styles.marginLeft48}>
                        <TouchableOpacity onPress={() => {
                            setParam(1);
                            }}>
                            <Image source={vovoJuju} style={styles.vovo}/>
                            <Text style={styles.legendasLoja}>Vovó Juju</Text>
                        </TouchableOpacity>
                    </View>

                    <View>
                        <TouchableOpacity onPress={() => {
                            setParam(2);
                            }}>
                            <Image source={mcJuju} style={styles.mc}/>
                            <Text style={styles.legendasLoja}>MC Juju</Text>
                        </TouchableOpacity>
                        </View>
                        
                    <View>
                        <TouchableOpacity onPress={() => {
                            setParam(3);
                            }}>
                            <Image source={patoJuju} style={styles.pato}/>
                            <Text style={styles.legendasLoja}>Pato Juju</Text>
                        </TouchableOpacity>
                    </View>

                </View>
            </BottomSheet>


I created the "let imagens", "const param", "const skin" and the "useEffect trying" to make this function. I already tried using the source in different ways such as source={skin} and source={imagens[skin]} but it havent worked.

CodePudding user response:

I'm not certain if this solves your problem, but here's how the first few lines of your component should look like without useEffect:


const imagens = {
  vovo: '../assets/vovoJuju.png',
  mc: '../assets/mcJuju.png',
  pato: '../assets/patoJuju.png',
};

export function Home(){

    const navigation = useNavigation<any>();
    const [showBottomSheet, setShowBottomSheet] = React.useState(false);
    const [param, setParam] = useState(1);
    const hide = () => {
      setShowBottomSheet(false)
    }
    function handleAbout(){
        navigation.navigate('About');
    }
    
    let skin = 'vovo';
    switch(param) {
       case 1: skin = 'vovo'; break;
       case 2: skin = 'mc'; break;
       case 3: skin = 'pato'; break;
    } 
    return /* the rest goes here */
    
}

To reference the actual image, you would use something like {imagens[skin]}.

I moved imagens outside of this function because it never changes, but it doesn't impact anything otherwise.

  • Related