如何在React Native中将Hooks用作图像的源?

huangapple go评论97阅读模式
英文:

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

问题

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 haven't worked.

我创建了"let imagens","const param","const skin"和"useEffect trying"来实现这个功能。我已经尝试过不同的方式来使用source,比如source={skin}和source={imagens[skin]},但都没有成功。

英文:

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.

答案1

得分: 1

这是代码的一部分,以下是翻译好的内容:

我不确定这是否解决了您的问题,但是您的组件的前几行应该如下所示,不使用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 /* 其余部分在此 */
    
}

要引用实际的图像,您可以使用类似{imagens[skin]}的方式。

我将imagens移到此函数外部,因为它永远不会更改,但否则不会影响任何内容。

英文:

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: &#39;../assets/vovoJuju.png&#39;,
  mc: &#39;../assets/mcJuju.png&#39;,
  pato: &#39;../assets/patoJuju.png&#39;,
};

export function Home(){

    const navigation = useNavigation&lt;any&gt;();
    const [showBottomSheet, setShowBottomSheet] = React.useState(false);
    const [param, setParam] = useState(1);
    const hide = () =&gt; {
      setShowBottomSheet(false)
    }
    function handleAbout(){
        navigation.navigate(&#39;About&#39;);
    }
    
    let skin = &#39;vovo&#39;;
    switch(param) {
       case 1: skin = &#39;vovo&#39;; break;
       case 2: skin = &#39;mc&#39;; break;
       case 3: skin = &#39;pato&#39;; 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.

huangapple
  • 本文由 发表于 2023年1月6日 12:32:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定