skip to Main Content

I want to change a value coming from API and its Boolean in React Native but even when I click on it the value is still false, but I want it to become notifications.read true on Press

Here is my code

                             `<TouchableOpacity
                                onPress={() => {
                                    {notifications.read == true}
                                    console.log(notifications.read)
                                }}
                            >
                                <Text style={{
                                    textDecorationLine: 'underline',
                                    fontSize: 14,
                                    color: Colors.primaryColor,
                                    lineHeight: 18,
                                   letterSpacing: 0.1,
                                }}>
                                   </Text>
                            </TouchableOpacity>`

3

Answers


  1. we use == when we want to check the value of the variable equal to something. instead use =

    `<TouchableOpacity
                                    onPress={() => {
                                        notifications.read = true
                                        console.log(notifications.read)
                                    }}
    
    Login or Signup to reply.
  2. const sample = () => {
      const [notifications, setNotifications] = useState({read:false});
      
      const handle_notif_status = () => {
        let new_status = {read:true};
        setNotifications(new_status);
      }
    
    }

    strong text

    now on your onPress method call handle_notif_status

    Login or Signup to reply.
  3. The {} in {notifications.read == true} is redundant and also the == makes this statement a comparison. Change this line to notifications.read = true to fix.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search