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`.
Probably not an answer but this one would correspond more closely to the caption:

```hs
tSumToN :: (Enum a, Fractional a) => Int -> a -> a
tSumToN n = sum . take n . tSeq

tSeq :: (Enum a, Fractional a) => a -> [a]
tSeq x = 
  interleave odds evens
  where
    odds = [ o / x | o <- [1,3..]]
    evens = [ x / e | e <- [2,4..]]
    interleave [] _ = []
    interleave (y:ys) zs = y:interleave zs ys

example :: Double
example = tSumToN 4 1.1

```

btw: this one does obviously not converge mathematically so it seems rather pointless to take partial sums - but hey whatever