# Add the Session to the State

**URL:** <https://discourse.sst.dev/t/add-the-session-to-the-state/136>\
**Category:** Chapter Comments\
**Created:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136 "2018-05-09T20:00:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/1 "2018-05-09T20:00:42Z")

</div>

_From @jayair on Mon Apr 10 2017 01:03:41 GMT+0000 (UTC)_  
  
Link to chapter - [https://serverless-stack.com/chapters/add-the-session-to-the-state.html](https://serverless-stack.com/chapters/add-the-session-to-the-state.html)  
  
_Copied from original issue: [https://github.com/AnomalyInnovations/serverless-stack-com/issues/39](https://github.com/AnomalyInnovations/serverless-stack-com/issues/39)_

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/2 "2018-05-09T20:00:43Z")

</div>

_From @KelpDuNord on Wed Nov 01 2017 15:43:39 GMT+0000 (UTC)_  
  
When running a Jest test against App.js, I get “Unexpected token” at the = after userHasAuthenticated in

```auto
userHasAuthenticated = authenticated => {
  this.setState({ isAuthenticated: authenticated });
}

```

The syntax seems correct according to the React docs. Anyone have a clue? Thanks.

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/3 "2018-05-09T20:00:43Z")

</div>

_From @jayair on Wed Nov 01 2017 17:38:06 GMT+0000 (UTC)_  
  
@KelpDuNord Yeah the syntax looks fine. I’m not sure why it’s complaining.

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/4 "2018-05-09T20:00:43Z")

</div>

_From @x11joe on Wed Nov 08 2017 09:39:10 GMT+0000 (UTC)_  
  
`<RouteNavItem key={1} href="/signup">` I noticed you added key={1} but the section does not explain why (or even if the reason why is because it will be used later). Curious what this is needed for?

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/5 "2018-05-09T20:00:44Z")

</div>

_From @jayair on Thu Nov 09 2017 18:23:36 GMT+0000 (UTC)_  
  
@x11joe React needs keys when you are creating a list of components. You can read more about it here - [https://reactjs.org/docs/lists-and-keys.html](https://reactjs.org/docs/lists-and-keys.html)

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/6 "2018-05-09T20:00:44Z")

</div>

_From @lucasgonze on Fri Nov 24 2017 23:30:57 GMT+0000 (UTC)_  
  
I find this bit of code particularly baffling:

`export default ({ component: C, props: cProps, ...rest }) => <Route {...rest} render={props => <C {...props} {...cProps} />} />;`

The combo of ES6 with React idioms in the context of shorthand code makes it inscrutable.

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/7 "2018-05-09T20:00:44Z")

</div>

_From @TheDagger on Fri Nov 24 2017 23:33:48 GMT+0000 (UTC)_  
  
@lucasgonze yeah I agree I had to spend like an hour wrapping my head around that the first time lol

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/8 "2018-05-09T20:00:44Z")

</div>

_From @jayair on Sun Nov 26 2017 18:27:34 GMT+0000 (UTC)_  
  
@lucasgonze @TheDagger Yeah I can see that. Let me add more of an explanation to that section.

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/9 "2018-05-09T20:00:45Z")

</div>

_From @jayair on Sun Nov 26 2017 19:04:46 GMT+0000 (UTC)_  
  
Just added a better explanation - [https://github.com/AnomalyInnovations/serverless-stack-com/commit/6be7f67ae12dd4619c842c8b0a86a282ce628fd2](https://github.com/AnomalyInnovations/serverless-stack-com/commit/6be7f67ae12dd4619c842c8b0a86a282ce628fd2)

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/10 "2018-05-09T20:00:45Z")

</div>

_From @x11joe on Sun Dec 10 2017 22:20:45 GMT+0000 (UTC)_  
  
I had one more question on this section, the code `<Route {...rest} render={props => <C {...props} {...cProps} />} />;` I get most of this now, but the part I don’t get in particular is `{...props} {...cProps} />}`. My understanding is this creates the properties and the values that go with the properties. This makes sense except for one thing. My understanding of attributes is it has to be formatted like this, `<a href="hey" custom="something">test</a>`. The **=** in particular here is where I am confused because I noticed that is not in the example but somehow it still works? How is this working with the equal omitted? The translation in my head looks like this right now `<C isAuthenticated true />` _In the case of App passing that in as a property_, does it not need equal in this case? Shouldn’t it be `<C isAuthenticated = true />` ? (I come html HTML/CSS/PHP, so this might be a newbie javascript question)

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/11 "2018-05-09T20:00:45Z")

</div>

_From @jayair on Mon Dec 11 2017 12:09:55 GMT+0000 (UTC)_  
  
@x11joe I agree this part is pretty confusing. And JSX has not been my favorite feature of React but it ends up being fairly convenient in practice.

In this specific case, your idea is right. The spread operator `{...props}` does it for you. Here is more info on it - [https://reactjs.org/docs/jsx-in-depth.html#spread-attributes](https://reactjs.org/docs/jsx-in-depth.html#spread-attributes). The entire chapter is worth a read if you want to get a better understanding of JSX.

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/12 "2018-05-09T20:00:46Z")

</div>

_From @teejK on Tue Jan 23 2018 16:22:41 GMT+0000 (UTC)_  
  
JS noob here: originally read `{component: C, props: cProps, ...rest}` as assigning values `C` and `cProps` to properties `component` and `props`, but `C` and `cProps` are being assigned as aliases for the properties `component` and `props`.

`export default ({ component: C, props: cProps, ...rest })`

> Finally, we take component (set as C) and props (set as cProps)

Could you name this behaviour so I can look up documentation for it or link to documentation for this behaviour so I can better understand how/when to use it?

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/13 "2018-05-09T20:00:46Z")

</div>

_From @jayair on Tue Jan 23 2018 17:16:24 GMT+0000 (UTC)_  
  
@teejK Yeah it’s called Destructuring [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring\_assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/14 "2018-05-09T20:00:46Z")

</div>

_From @kevinlbatchelor on Thu Jan 25 2018 06:49:32 GMT+0000 (UTC)_  
  
Writing unreadable code like this is ridiculous, we write code for people to read not computers. I’ve been coding for 12 years, writing react since its release, when I saw this I laughed, then came here to vent. I run in to a lot of devs who do this to look smart under the guise that it is efficient.

export default ({ component: C, props: cProps, …rest }) =\> \<Route {…rest} render={props =\> \<C {…props} {…cProps} /\>} /\>;

The spread operator is still tricky, but super handy. here is the same code written more verbosely, and perhaps easier to read, which IMO should be apart of any good tutorial

```javascript
<AppliedRoute path="/login" exact component={Login} props={childProps}/>

```

```javascript
 
import React, {Component} from 'react';
import {Route} from 'react-router-dom';

export default class AppliedRoute extends Component {
    render() {
        const Comp = this.props.component;
        const childProps = this.props.props;
        const exact = this.props.exact;
        const path = this.props.path;

        return (
            <Route path={path} exact={exact} render={(routerProps) => {
                return (
                    <Comp {...routerProps} {...childProps}/>
                    //spread operator is adding all properties of routerProps & childProps to Comp
                   // i.e. <Comp match={match} location={location} whatever={whatever}>
                   //this is useful because we cannot predict what gets passed in to childProps, and we probably dont know what is in routerProps because it is a library
                );
            }}/>
        );
    }
 };
```

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/15 "2018-05-09T20:00:47Z")

</div>

_From @jayair on Thu Jan 25 2018 20:44:25 GMT+0000 (UTC)_  
  
@kevinlbatchelor While what you are saying is true; I get plenty of comments from other folks who look at a simplified version and tell us that we should use the “modern version” because it’s more “efficient”.

That said, I do think code like this (as it is in a lot of other languages) will become more common as it gets adopted. There are going to be some growing pains for the JS world. In fact, this line is taken from the React Router docs which I assume some of our readers will try and go over.

This is why I decided to leave it this way but added a long explanation on how some of destructuring parts work - [https://github.com/AnomalyInnovations/serverless-stack-com/commit/6be7f67ae12dd4619c842c8b0a86a282ce628fd2](https://github.com/AnomalyInnovations/serverless-stack-com/commit/6be7f67ae12dd4619c842c8b0a86a282ce628fd2)

Thanks for your comment. It’ll help anybody else that is looking for a simpler way to write this. And I understand your frustration.

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/16 "2018-05-09T20:00:47Z")

</div>

_From @twalkerjr22 on Sun Apr 08 2018 02:38:23 GMT+0000 (UTC)_  
  
Does anyone know how to fix this error? TypeError: \_this.props.userHasAuthenticated is not a function. (In ‘\_this.props.userHasAuthenticated(true)’, ‘\_this.props.userHasAuthenticated’ is undefined)

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/17 "2018-05-09T20:00:47Z")

</div>

_From @jayair on Mon Apr 09 2018 17:27:01 GMT+0000 (UTC)_  
  
@twalkerjr22 Where exactly are you seeing this?

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/18 "2018-05-09T20:00:47Z")

</div>

_From @caronicolas on Mon Apr 16 2018 12:16:10 GMT+0000 (UTC)_  
  
I have the same error as @twalkerjr22 . The error appear in a popup when I try to log in.  
I have read again the complete part two times but I don’t find what I’m doing wrong…

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/19 "2018-05-09T20:00:48Z")

</div>

_From @jayair on Mon Apr 16 2018 18:04:31 GMT+0000 (UTC)_  
  
@caronicolas Can you tell me exactly after which step in the tutorial do you start seeing this error?

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:00pm UTC](https://discourse.sst.dev/t/add-the-session-to-the-state/136/20 "2018-05-09T20:00:48Z")

</div>

_From @alex-romanov on Mon Apr 16 2018 19:03:44 GMT+0000 (UTC)_  
  
@jayair, I have the same error as @carnicolas, the error is comes from within the try statement in the handleSubmit function in Login.js

[Next page](https://discourse.sst.dev/t/add-the-session-to-the-state/136.md?page=2)
