0

The problem is the title.

I am using:

  1. Typescript Node
  2. Typescript React

I am trying to pass a setPage from useState to the subroutine called subRenderSwitch which decides which form to display, while renderSwitch decides which navigation bar to display.

Main App

function App() {
  let [page,setPage] = useState("")
  let firstRender = useRef(true);

  const subRenderSwitch = (str: any) => {
    switch (str) {
      case 'Login':
        return <Landing setPage={setPage} />
      case 'Register':
        return <Register />
      case 'Update Password':
        return <UpdatePassword />
      default:
        return <Landing />
    }
  }

  const renderSwitch = (str: any) => {
    switch(str) {
      case 'Not Logged':
        return {head: <Nav setPage={setPage} />, body: subRenderSwitch(page)}
      case 'Logged':
        return {head: '', body: ''}
      default:
        return {head: <Nav setPage={setPage} />, body: subRenderSwitch(page)}
    }
  }

  return (
    <div className="App">
      {renderSwitch(page).head}
      {renderSwitch(page).body}
    </div>
  );
}

export default App;

export default function Nav ({ setPage }: any) {
    let [nav,setNav] = useState('')

    const clickHandler = (e: any, str: any) => {
        e.preventDefault()

        const sendData = async () => {
            ...
        }
        sendData().then(res => setNav(res.data.value))

        return 
    }
    
    useEffect(() => {
        setPage(nav) // NO ERROR HERE
    },[nav,setPage])
    
    const renderSwitch = () => {
        switch (nav) {
            ...
        }
    }

    return (
        <div id='navBar'>
        {renderSwitch()}
        </div> 
    )
}

export default function Landing ( { setPage }: any ) {
    let [message,setMessage] = useState("Enter details to Login")
    let [data,setData] = useState({email: '',pass: ''})
    let [nav,setNav] = useState('')
    let firstRender = useRef(true)

    const submitHandler = (e: any) => {
        e.preventDefault()
        
        const sendData = async () => {
           ...
        }
        sendData().then(res => {
            if (res.data != null) {
                setMessage("User Found")
                setNav('Login')
            }
            else {
                setMessage("User Not Found")
            }
        })

        return
    }
    
    useEffect(() => {
        if (firstRender.current) {
            firstRender.current = false
            return
        }
        setPage(nav) // ERROR CAUSED HERE
    },[nav,setPage])

    return (
        <div id='landingPage'>
            ...
        </div>
    )
}

Please know that I am still a newbie and therefore the above may not follow any best practices. Additionally, I will accept any critique that may improve my code.

I can successfully pass setPage to renderSwitch without any trouble, but for subRenderSwitch I get setPage is not a function.

What could be the cause for this error?

Xon Doi
  • 43
  • 2
  • The `default` in your `subRenderSwitch` is `return `. That doesn't set `setPage`. You follow that branch in the `default` branch of `renderSwitch` when `page` is `""`. – T.J. Crowder Feb 25 '22 at 10:25
  • Your best bet for this kind of thing is to use the debugger built into your IDE and/or browser and step through the code, watching the variables as you go. Using a debugger is not an advanced thing, it's one of the first things any new coder should learn, since it makes learning everything else so much easier. [More here](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – T.J. Crowder Feb 25 '22 at 10:26
  • Side note: It seems odd to pass `page` to `renderSwitch` and have `renderSwitch` use the page you pass it (`str`) for its `switch`, but then instead of passing on `str` (the page it received), it passes `page` directly to `subRenderSwitch`. While that should work, it's asking for trouble -- `renderSwitch` should probably pass `str` instead. – T.J. Crowder Feb 25 '22 at 10:29
  • 1
    @T.J.Crowder Thanks for the side-note! Will definitely fix. Your first comment did it for me, sometimes you just need a second pair of eyes :) – Xon Doi Feb 25 '22 at 10:46

0 Answers0