0

I have a simple state let [isOpen, setIsOpen] = useState(false);

It doesn't when it gets back set to false it the ternary operator doesn't seem to be doing anything.

export default function ShowContact(props) {
  let [isOpen, setIsOpen] = useState(false);

...


function closeButtons() {
    setIsOpen(false);
    console.log(isOpen);
  }

I have the following

   {!isOpen ? <span>TRUE</span> : <span>FALSE</span>}

      {!isOpen ? (
        <div>
          <div className="mt-6 flex flex-col">
            <button className="mb-1 rounded bg-blue-500 px-4 py-3 capitalize text-white duration-100 ease-in-out hover:bg-blue-700">
              <span className="text-center">edit</span>
            </button>
            <button
              className="rounded bg-red-500 px-4 py-3 capitalize text-white duration-100 ease-in-out hover:bg-red-700"
              onClick={closeButtons}
            >
              <span className="text-center tracking-wide">delete</span>
            </button>
          </div>
        </div>
      ) : (
        <div>
          <div className="mt-6 flex flex-col">
            <button className="mb-1 rounded bg-green-500 px-4 py-3 capitalize text-white duration-100 ease-in-out hover:bg-green-700">
              <span className="text-center">edit</span>
            </button>
            <button
              className="rounded bg-red-500 px-4 py-3 capitalize text-white duration-100 ease-in-out hover:bg-red-700"
              onClick={deleteContact}
            >
              <span className="text-center tracking-wide">yes delete</span>
            </button>
          </div>
        </div>
      )}

the {isOpen ? <span>TRUE</span> : <span>FALSE</span>} is simple to test that isOpen is being set to false. Which is does as I have checked the console.

enter image description here

Still though the ternary isn't changing. This is very annoying as it was working earlier and something has changed I am not sure if this is an effect from other components, but I can not see why?

But I know the buttons are working as the deleteContact function is working and the isOpen is getting set to false

I am complete miffed about this, any suggestions would be welcome.

import { useEffect, useState } from 'react';

export default function ShowContact(props) {
  let [isOpen, setIsOpen] = useState(false);
  let [contact, setContact] = useState({});

  // delete contact from api then reloads the page
  function deleteContact() {
    fetch(`http://localhost:8000/delete-contact/${props.contactId}`, {
      method: 'DELETE',
    }).catch((err) => console.log(err));

    setIsOpen(false);
    window.location.reload(true);
  }

  // get contact from api by contact id
  useEffect(() => {
    async function fetchContact() {
      await fetch(`http://localhost:8000/get-contact/${props.contactId}`)
        .then((response) => response.json())
        .then((data) => {
          setContact(data);
        })
        .catch((err) => console.log(err));
    }

    if (props.open) {
      fetchContact();
    }
  }, []);

  function closeButtons() {
    setIsOpen(false);
    console.log(isOpen);
  }

  function openButtons() {
    setIsOpen(true);
    console.log(isOpen);
  }

  return (
    <div className="bg-gray-100 p-4">
      <div>
        {/* <div className="mx-auto my-4 block w-fit rounded-full bg-gray-300 p-6 ">
          ...
        </div> */}
        <div className="flex flex-col text-center">
          <span className="font-bold">
            {contact.first_name} {contact.last_name}
          </span>
          <span className="text-sm text-gray-400">{contact.company}</span>
        </div>

        {contact.telephone !== '' || contact.telephone === 'NULL' ? (
          <div className="my-4 flex flex-col rounded-md bg-white p-4 text-left">
            <span className="text-sm font-medium">telephone</span>
            <a
              href={`tel:${contact.telephone}`}
              className="text-sm text-blue-500"
            >
              {contact.telephone}
            </a>
          </div>
        ) : null}

        {contact.email !== '' || contact.email === 'NULL' ? (
          <div className="my-4 flex flex-col rounded-md bg-white p-4 text-left">
            <span className="text-sm font-medium">email</span>
            <a
              href={`mailto:${contact.email}`}
              className="text-sm text-blue-500"
            >
              {contact.email}
            </a>
          </div>
        ) : null}

        {contact.address !== '' || contact.address === 'NULL' ? (
          <div className="my-4 flex flex-col rounded-md bg-white p-4 text-left">
            <span className="text-sm font-medium">address</span>
            <span className="text-sm text-blue-500">{contact.address}</span>
          </div>
        ) : null}

        {contact.notes !== '' || contact.notes === 'NULL' ? (
          <div className="my-4 flex flex-col rounded-md bg-white p-4 text-left">
            <span className="text-sm font-medium">notes</span>
            <span className="text-sm">{contact.notes}</span>
          </div>
        ) : null}
      </div>

      {!isOpen ? <span>TRUE</span> : <span>FALSE</span>}

      {!isOpen ? (
        <div>
          <div className="mt-6 flex flex-col">
            <button className="mb-1 rounded bg-blue-500 px-4 py-3 capitalize text-white duration-100 ease-in-out hover:bg-blue-700">
              <span className="text-center">edit</span>
            </button>
            <button
              className="rounded bg-red-500 px-4 py-3 capitalize text-white duration-100 ease-in-out hover:bg-red-700"
              onClick={closeButtons}
            >
              <span className="text-center tracking-wide">delete</span>
            </button>
          </div>
        </div>
      ) : (
        <div>
          <div className="mt-6 flex flex-col">
            <button className="mb-1 rounded bg-green-500 px-4 py-3 capitalize text-white duration-100 ease-in-out hover:bg-green-700">
              <span className="text-center">edit</span>
            </button>
            <button
              className="rounded bg-red-500 px-4 py-3 capitalize text-white duration-100 ease-in-out hover:bg-red-700"
              onClick={deleteContact}
            >
              <span className="text-center tracking-wide">yes delete</span>
            </button>
          </div>
        </div>
      )}
    </div>
  );
}

Update with the whole component. I am checking for isOpen to be false as I want to set some delete buttons, but then confirm the delete once the first set of buttons set isOpen to true

mrpbennett
  • 1,527
  • 15
  • 40
  • 2
    In the code, there's a `!` which inverts the boolean value. `false` becomes `true` with `!isOpen`. – Emile Bergeron Feb 15 '22 at 16:45
  • [This](https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately) is relevant, but it's really hard to tell what your question is actually about. Is it about the console output? Then yes, that's the problem. Something else? Please show us via with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button). Stack Snippets support React, including JSX; [here's how to do one](http://meta.stackoverflow.com/questions/338537/). – T.J. Crowder Feb 15 '22 at 16:46
  • 1
    Side note: `!isOpen ? TRUE : FALSE` is backward, showing `TRUE` when `isOpen` is `false` and vice-versa. **Edit:** Actually, it's not a side note, you're doing the same thing when you render, rendering the close button when `isOpen` is **already** false, as @EmileBergeron pointed out above. – T.J. Crowder Feb 15 '22 at 16:46
  • I have added the whole component...I want to render something like a delete button when isopen is false. But then the user clicks delete I want it to set `isOpen` to true and display another set of buttons which would have the `deleteContact` onclick – mrpbennett Feb 15 '22 at 16:54
  • Guys! Sorry...silly mistake it was the ! – mrpbennett Feb 15 '22 at 16:56
  • actually removing the ! worked, but now when I clicked the button to display the other buttons I just get booted out of the component – mrpbennett Feb 15 '22 at 17:02
  • full repo here is if you want to try and reproduce https://github.com/mrpbennett/contact_fastapi/tree/react-fe – mrpbennett Feb 15 '22 at 17:06

0 Answers0