0

I would like to change the onPressOut field from React Native's Pressable component based on the state I pass in. But when I log it, it keeps giving me stale state and I'm not sure why. I would expect it to print true when the toggle is active and false when it isn't, but it just prints out false no matter what.

App.tsx

export default function App() {
    const [isActive, setIsActive] = useState(false);

    const toggleIsActive = () => {
        setIsActive(!isActive);
    };

    return (
        <View style={styles.container}>
            <Switch value={isActive} onValueChange={toggleIsActive} />
            <MyButton isActive={isActive} />
        </View>
    );
}

Button component

export const MyButton = ({ isActive }: { isActive: boolean }) => {
    return (
        <Pressable
            style={{
                width: 100,
                height: 100,
                backgroundColor: "red",
            }}
            onPressOut={() => {
                console.log(isActive);
            }}
        />
    );
};

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.