title: React data retrieval - prevent multiple calls to database
body: <p>I have used react to create a website that retrieves data from a database, using REST API.
  On my home page, <strong>all</strong> data will be retrieved with useEffect and stored in a state.
  When the user searches in the form, it will match the data and send the user along with the found data to the corresponding page.</p>
  <p>However, I would also like the user to directly get to the page by typing in the url. If a user bypasses the home page and goes directly to the specific page's url, then the searched page will not have the data(Assuming data was passed to the specific page from the home page, using props).</p>
  <p>My temporary solution is to search the database every time a user switches between pages.</p>
  <p>Is there a better solution such that the website has to only load in the data once?</p>
  <p><strong>Summary:</strong></p>
  <p>I would like to</p>
  <ol>
  <li>Let user search from home page using textfield</li>
  <li>Let user directly type in url to specific page (ex: websiteName/notes/myfirstnote)</li>
  <li>Prevent retrieval of data from database for every visited page (for efficiency purposes)</li>
  </ol>
  <p>Home page code:</p>
  <pre><code>const Home = () =&gt; {
    const navigate = useNavigate()
    const [searchedNote, setSearchedNote] = useState('')
    const [allNotes, setAllNotes] = useState([])
  
    useEffect(() =&gt; {
      console.log(&quot;Home Page&quot;)
      fetch('databaseURL')
      .then(response =&gt; response.json())
      .then(data =&gt; setAllNotes(data.Items))
      .catch((error) =&gt; {
        console.error(error)
      })
    }, [])
  
    const searchNote = (event) =&gt; {
      //Omitting code to find the searched note
      navigate(`/notes/${foundNote.name}`)
    }
  
    //Rest of the code returns HTML code for Home
    //This includes the textfield to find a specific 'note'
    //The onsubmit function of the textfield is 'searchNote' which is stated above
  }
  export default Home
  </code></pre>
  <p>Notes page code:</p>
  <pre><code>const Notes = () =&gt; {
    const [allNotes, setAllNotes] = useState([])
    const [note, setNote] = useState()
  
    useEffect(() =&gt; {
      console.log(&quot;Welcome Notes&quot;)
  
      //Must get all data from database again. I would like to avoid doing this.
      fetch('databaseUrl')
      .then(response =&gt; response.json())
      .then(data =&gt; setAllNotes(data.Items))
      .catch((error) =&gt; {
        console.error(error)
      })
    }, [])
  
    //After getting database, filter out the 'note' of interest using the url
    //(ex: if url is websiteName/notes/myfirstnote, then search allNotes for name == 'myfirstnote'
  
    //Do things with the found note here
  
    //Rest of the code returns HTML code for Notes
  }
  export default Notes
  </code></pre>