改变 React Native 中 Pressable 点击时的样式

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

Change Style on Pressable Click React Native

问题

我正在尝试在React Native中使用Pressable创建自定义按钮,但无法在按下时更改样式。每当我按Pressable并更改样式属性时,它们会在控制台中打印出更改,但在应用程序中没有反映。

import { useState } from "react";
import { Pressable, StyleSheet, Text, View } from "react-native";

export default function Button(props) {
    const [refresh, setRefresh] = useState(10);

    function setBG() {
        styles.container.backgroundColor = 'red';
        setRefresh(1);
        console.log(styles.container.backgroundColor);
        console.log('called');
    }
    
    return (
        <Pressable onPress={setBG}>
            <View style={styles.container}>
                <Text style={styles.buttonLabel}>{props.text}</Text>
            </View>
        </Pressable>
    )
}

const styles = StyleSheet.create({
    container: {
        backgroundColor: 'blue',
        minWidth: 100,
        minHeight: 50,
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center'
    },
    buttonLabel: {
        color: 'white',
        fontWeight: 700
    }
})
英文:

I am trying to create a custom button using pressable in react native but I cant change the style on press. Whenever I press on the pressable and change the style properties they reflect the change when I print it in the console but it isn't reflected in the app.

import { useState } from &quot;react&quot;;
import { Pressable, StyleSheet, Text, View } from &quot;react-native&quot;;


export default function Button(props) {
    const [refresh, setRefresh] = useState(10);

    function setBG() {
        styles.container.backgroundColor = &#39;red&#39;;
        setRefresh(1);
        console.log(styles.container.backgroundColor);
        console.log(&#39;called&#39;);
    }
    
    return (
        &lt;Pressable onPress={setBG}&gt;
            &lt;View style = {styles.container}&gt;
                &lt;Text style = {styles.buttonLabel}&gt;{props.text}&lt;/Text&gt;
            &lt;/View&gt;
        &lt;/Pressable&gt;
    )
}

const styles = StyleSheet.create({
    container: {
        backgroundColor: &#39;blue&#39;,
        minWidth: 100,
        minHeight: 50,
        display: &#39;flex&#39;,
        alignItems: &#39;center&#39;,
        justifyContent: &#39;center&#39;
    },
    buttonLabel: {
        color: &#39;white&#39;,
        fontWeight: 700
    }
})

答案1

得分: 4

你可以传递一个样式的函数,该函数接收一个布尔值,指示当前是否被按下,并返回相应的样式。

https://reactnative.dev/docs/pressable#style

你也可以将一个渲染函数作为Pressable的子组件传递,该函数接收一个布尔值,指示当前是否被按下,并返回相应的子元素。

https://reactnative.dev/docs/pressable#children

使用其中一个或两者都可以为按下和未按下状态呈现相应的样式。

不需要尝试自行管理状态。Pressable会将其提供给你。

英文:

You can pass a function for style that receives a boolean indicating whether it’s currently pressed and return the appropriate styles.

https://reactnative.dev/docs/pressable#style

You can also pass a render function as a child of pressable that receives a boolean indicating whether it’s currently pressed and returns the appropriate children.

https://reactnative.dev/docs/pressable#children

Using either or both of these you can render the appropriate state for pressed and unpressed.

No need to try to manage the state yourself. Pressable will hand it to you.

huangapple
  • 本文由 发表于 2023年6月26日 02:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551921.html
匿名

发表评论

匿名网友

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

确定