I am trying to pass the toggleState value from my Sidebar.jsx as props to another file Screens.jsx, which toggles between displaying the homepage or displaying nothing based on the toggleState value. However, when I try to access the value of the props in Screens.jsx, it tells me the value is undefined.
Sidebar.jsx
import Screens from './Screens';
import {useState} from 'react';
function Sidebar() {
const [toggleState, setToggleState] = useState(1);
return (
<div>
<div
onClick={() => setToggleState(1)}
>
</div>
<div
onClick={() => setToggleState(2)}
>
</div>
<Screens toggleState={toggleState} />
</div>
);
};
export default Sidebar;
Screens.jsx
import { useState, useEffect } from 'react';
import Homepage from './homepage';
function Screens({toggleState}) {
const [isActive, setIsActive] = useState(1);
console.log(toggleState);
useEffect(() => {
setIsActive(toggleState);
});
return (
<div style={{ height: 752 }}>
{ isActive === 1 ? <Homepage /> : null }
</div>
);
};
export default Screens;
Both Sidebar and Screens are imported in my Home.jsx. I'm not sure if this might be causing the issue.
Home.jsx
import Sidebar from '../components/sidebar';
import Screens from '../components/screens';
function Home() {
return (
<div>
<div style={{ display: 'flex', width: 1200, height: 792 }}>
<div style={{ width: 71, height: 792, marginTop: -42, marginLeft: -10, overflow: 'hidden' }}>
<Sidebar />
</div>
<div style={{ display: 'flex', flexDirection: 'column', width: 670, height: 752, backgroundColor: '#1e203c' }}>
<Screens />
</div>
</div>
</div>
);
};
export default Home;