0

I'm trying to pass a hook state to another component inside the Axios function. The state has the initial value to false, but when the method post happens to Axios, inside then() I made the state to true which in this case is setShowFlagCreate(true) Im trying to pass this state to the UserList.js component, but I don't know-how. After I pass it, I wanna use this state to show the flag(which says a user has been created.) on the UserList.js page

How can I do this?

UserCreate.js

export default function UserCreate({}) {

const [showFlagCreate, setShowFlagCreate] = useState(false)

const save = async () => {

    const UserParams = {
      userName:
        currentUser
      Gender: gender,
      country: USA,
    };
 
    await axios
      .post(
        process.env.REACT_APP_URL, userParams
      )  
      .then((response) => {
        navigate("/userlist");
        setShowFlag(true); //I want this state to pass it on the UserList component as true
      })
      .catch((error) => {
        console.log(error);
      });
  

    };
}
return(
  <div>
  </div>
)

UserList.js

export default function UserList() {

  const [showFlagCreate, setShowFlagCreate] = useState(false)

useEffect(() => {
    axios
      .get(process.env.REACT_API_URL + usersParams) //geting the user form db
      .then((response) => {
        response.data.forEach(function (item) {
          let newUser = {
            id: user.id,
            name: Username,
            gender: "male",
            description: "stuff",
          };
        });
      })
      .catch((error) => {
        console.log(error);
      });
  }, []);

  return (
    <div>
        <FlagCreate show={showFlagCreate} setShow={setShowFlagCreate}/> //passing the props
     </div>
);
}

I want to get the state in UserList component and show the flag since the state is true.

FlagCreat.js

export default function FlagCreate({ show, setShow }) {
 <div>
     <div>
        <p>The user that you have been created is saved.</p>
    </div>
    <div>
      <button onClick={() => {setShow(false)}}>
          <span >Close</span>
      </button>               
 </div>
}

The idea is that after the setShowFlagCreate turned to true in UserCreate.js , the flag has to be shown in the UserCreate component . How can I make it work?

Zani123
  • 210
  • 2
  • 11
  • What navigation library are you using? – Vencovsky Feb 16 '22 at 13:05
  • @Vencovsky this ```import { useNavigate } from "react-router-dom";``` – Zani123 Feb 16 '22 at 13:07
  • If on a successful response you always navigate to UserList component, you can just provide a query parameter with the value and you won't even need to use state here. Navigate like this `navigate("/userlist?showFlagCreate=true");` and then read the value of query param inside UserList component – cEeNiKc Feb 16 '22 at 13:15
  • And when you want to set the `show` to false you can simply remove the query param from the url – cEeNiKc Feb 16 '22 at 13:18

2 Answers2

1

What you should do is pass the state when calling navigation retrieve that state in the UserList.

UserCreate

navigate("/userlist", { state: { showFlag: true } });

UserList

const { state } = useLocation();
const { showFlag } = state; // use showFlag the way you want.

You can understand more how it works with this answer

Vencovsky
  • 28,550
  • 17
  • 109
  • 176
0

you could define this state in contextApi and share it everywhere you need - you can read about it here

Hen Moshe
  • 124
  • 6