0

I having two files header.js and upload.js. In upload.js I have a popup modal which should be open when I click upload button in the header.js

So basically I want to call setOpened function present in upload.js from header.js

header.js

import React from 'react'

const Header = () => {
  return (
    <div>
          <Link
             data-bs-toggle="modal"
             onClick={() => setOpened(true)}
             role="button"
            >Upload</Link>
    </div>
  )
}

export default Header

upload.js

import React from 'react'
const [opened, setOpened] = useState(false);

const Upload = () => {
  return (
    <div>
      <Modal
        opened={opened}
        onClose={() => setOpened(false)}
        title="Introduce yourself!"
      >
        {/* Modal content */}
      </Modal>
    </div>
  )
}

export default Upload

In App.js

import Header from './component/Header';
import Footer from './component/Footer';
import Upload from './component/Upload';

function App() {
  return (
   <Header/>
       <Routes>
         <Route path="/upload" element={<Upload/>}/>
       </Routes>
   <Footer/>
);
}

export default App;
5
  • Is upload.js used only for the purpose of displaying a modal? Commented May 10, 2022 at 15:29
  • Not Exactly. It having more functionalities also. Commented May 10, 2022 at 15:30
  • 2
    just lift state up or use context or use some state management tool(redux, mobx) reactjs.org/docs/lifting-state-up.html reactjs.org/docs/context.html Commented May 10, 2022 at 15:32
  • 1
    You can't call const [opened, setOpened] = useState(false); from outside the component, that would mean it will only be created once, not matter how many Upload components you instantiate but it's not allowed. It should be in the function itself. Commented May 10, 2022 at 15:33
  • Ok Thank You, I have to try another way. Commented May 10, 2022 at 15:38

1 Answer 1

4

To echo @I-vasilich-I's comment about lifting state up, you should move your modal open state to a higher level (like in App) and pass that state to each child that needs it.

For example

const Header = ({ openModal }) => (
  <div>
    <Link data-bs-toggle="modal" onClick={openModal} role="button">
      Upload
    </Link>
  </div>
);

const Upload = ({ opened, closeModal }) => (
  <div>
    <Modal opened={opened} onClose={closeModal} title="Introduce yourself!">
      {/* Modal content */}
    </Modal>
  </div>
);

const App = () => {
  const [opened, setOpened] = useState(false);
  return (
    <>
      <Header openModal={() => setOpened(true)} />
      <Routes>
        <Route path="/upload" element={<Upload opened={opened} closeModal={() => setOpened(false)} />} />
      </Routes>
      <Footer />
    </>
  );
};

(note make sure you add imports)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.