I wanna hide a component based on if something happens in another component.
this is my code in app:
function App() {
const [user] = useAuthState(auth);
const [hasUsername, setHasUsername] = useState(false);
if (user) {
return (
<div className="App">
<header>
<SignOut />
{hasUsername ? <ShowUsername uid={user.uid} /> : <RegisterUsername />}
</header>
<section>{<ChatRoom />}</section>
</div>
);
} else {
return (
<div className="App">
<header></header>
<section>{<SignIn />}</section>
</div>
);
}
}
and this in show component:
class ShowUsername extends Component {
constructor(props) {
super(props);
this.state = { username: "", uid: this.props.uid };
}
async componentDidMount() {
this.setState({ message: "loading..." });
const usernameRef = await firestore
.collection("username")
.where("uid", "==", this.state.uid)
.get();
var username = "Not registered yet";
usernameRef.docs.map((doc) => {
if (doc.exists) {
username = "Logged in as: " + doc.data().username;
setHasUsername(true); //issue is here
}
});
this.setState({ username: username });
}
render() {
let { username } = this.state;
return <div>{username}</div>;
}
}
what I want is that when I call setHasUsername(true)
it should change the hasusername
to true and show the ShowUsername
or RegisterUsername
depending if its true
or false
. what is the best approach to this?