0

This is my App.js

function App() {
   const [patient, setPatient] = useState("1")

  return (
      <div className="App" >

        <Router>
          <Switch>
            <DepNamesContext.Provider value={{patient, setPatient}}  > 
            <Route exact path="/" >
              <HomePage   />
            </Route>
            <Route path="/EmployeeSurvey/:email/:firstName/:lastName/:code">
              <NormalSurvey />
            </Route>
            <Route  path="/VisitorSurvey/:email/:firstName/:lastName/:code" >
              <VisitorSurvey />
            </Route>
            <Route path="/NewVisitorSurvey/:email/:firstName/:lastName/:code">
              <NewVisitorSurvey />
            </Route>
            <Route path="/ConsentPage/:email/:firstName/:lastName/:code/:companyName">
              <Consent />
            </Route>
            <Route path="/Pass/:code/:name/:email">
              <Pass />
            </Route>
            <Route path="/Fail/:code">
              <Fail />
            </Route>
          
          </DepNamesContext.Provider> 

          </Switch>
        </Router>
       
      </div>
    );
  } 
export default App;

This is a portion from Homepage.js

import { DepNamesContext } from "./DepNamesContext"
   const {setPatient,patient} = useContext(DepNamesContext);

I'd use setPatientMinor("3") and it will get updated in the homepage.js component but when I try and access patient state in VisitorSurvey.js Component, it's still the initial value of "1"

nnmast
  • 31
  • 3
  • Suggestion: See if you can reduce the size of the example. See https://stackoverflow.com/help/minimal-reproducible-example – TrueWill Mar 29 '21 at 16:41
  • That's not how Context works afaik: https://reactjs.org/docs/hooks-reference.html#usecontext A context's value is changed by changing what you pass as `value` into the provider. –  Mar 29 '21 at 16:43
  • Is there a reason why you put Provider inside Switch? *when I try and access patient state in VisitorSurvey.js Component, it's still the initial value of "1"* - what are both comps? – Estus Flask Mar 29 '21 at 16:43
  • @ChrisG That makes sense, i'm quite new to ReactJs, thank you. do you have any suggestions to how I could solve the issue. I tried using Mobx and pullstate but it didn't work. – nnmast Mar 29 '21 at 16:47
  • @EstusFlask No reason, I could put it outside but it doesn't solve the problem – nnmast Mar 29 '21 at 16:47
  • Duplicate: [How to update React Context from inside a child component?](https://stackoverflow.com/questions/41030361/how-to-update-react-context-from-inside-a-child-component) –  Mar 29 '21 at 16:55

0 Answers0