Question: I am new to react and struggling to change the state of a component, and on the change replace the contents.

I have the following:

```js
function App() {

 const [page, setPage] = React.useState('list');

 switch(page) {
  case 'A':
   return(
   <body> 
    <Home />
    <A />
   </body>
  )
  case 'B':
  return(
   <body> 
    <Home />
    <B />
   </body>
  )
 }
}

function A() {

 return (
  <div>
   <div onClick={() => setPage('B')}>GoToB</div>
  </div>
 )
}

function B() {
    return (
       <div>test</div>
    )
}

```

There is a separate `Home` component that displays properly. How do I correctly call the `setPage` function to change the state so that the switch case is triggered and changes out the component?

Answer: 
Other answers have already explained the errors here. A nice Haskell way to solve this problem is as follows:

```hs
import Data.List (scanl')

getNthApprox n x = approximations !! n where
    approximations = scanl' (+) 0 $ fmap term [1..]
    term n = if n `mod` 2 == 0
             then x / fromIntegral n
             else fromIntegral n / x

```

It turns out that due to the magic of laziness, `getNthApprox` works out to have the same performance characteristics as tail recursion. This is because elements of `scanl' (+) 0 $ fmap term [1..]` are constructed only as they are needed in the calculation of `approximations !! n`.
What you apparently meant was

```
recursion n x = if snd (properFraction (n / 2)) > 0   -- isOdd
                then x/n + recursion (n-1) (x**(-1))
                else n/x + recursion (n-1) (x**(-1))

```

but there's two problems here. First of all *correctness*, since you start from `n`th term and go back to `0`th, but use `x` as the starting value always, whereas it differs depending on whether `n` is even or odd. We fix problems like this by moving the actual work into the inner, "worker" function, and fixing the starting value of the iteration.

Second problem is that it is *not* *tail recursive*. We fix problems like that by introducing an accumulating parameter to the inner, "worker" function. One tool to fix two problems!

```
-- T = 1/x + x/2 + 3/x + x/4 + ...

recursion :: RealFrac a => a -> a -> a
recursion n x = if snd (properFraction (n / 2)) > 0   -- n is odd
                then goOdd  n 0
                else goEven n 0
  where
  goOdd  n acc     = goEven (n-1) (acc + n/x)
  goEven n acc 
       | n <= 0    = acc
       | otherwise = goOdd  (n-1) (acc + x/n)

```

Now it's correct, and tail recursive as you wanted.