# hooks

**URL:** https://es.discourse.group/t/hooks/1035
**Category:** 💡 Ideas
**Tags:** proposal
**Created:** [October 18, 2021, 8:05am UTC](https://es.discourse.group/t/hooks/1035 "2021-10-18T08:05:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![guaijie](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/guaijie/32/1068_2.png) [@guaijie](https://es.discourse.group/u/guaijie)
#### Post date: [October 18, 2021, 8:05am UTC](https://es.discourse.group/t/hooks/1035/1 "2021-10-18T08:05:04Z")

</div>

This is just a rudimentary idea, and I hope you can get active and probe together.

## proposal: hooks

```js

function name() {

  // useState(get, set);

  // readOnly 
  // Avoid 'closure trap'
  const { getName } = useState(() -> getFirstName() + getLastName());

  // It doesn't have to be in order

  const { getLastName, setLastName } = useState((v)->v, v -> v ?? 'initial');

  const {getFirstName, setFirstName} = useState((v)->v, v -> v ?? 'initial'); 

  

  return getName();

}

```

```js

function user(id){

  // initial load

  // useState(initialValue, set)

  const {getUser, setUser} = useState({}, async (id)->{

    return await getUser(id);

  });

  

  setUser(id) // user's id 

  return getUser();

}

const user = await user(id);

```

---

<div class="post-metadata">

### Author: ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)
#### Post date: [October 18, 2021, 3:07pm UTC](https://es.discourse.group/t/hooks/1035/2 "2021-10-18T15:07:21Z")

</div>

What exactly would happen from the caller of user()'s perspective in the second example?

Would I receive an empty object, that magically transforms itself into a populated object once the async operation to fetch user data completes?

The idea of hooks works really with in React due to the nature of how their render functions work. It works great to help dynamically alter the shape of the returned JSX structure. I'm not sure it'll be as useful as an object manipulation mechanism, but if you can think of any specific use cases for it, I'd love to hear those, that could help us understand how such a feature could be useful here.

---

<div class="post-metadata">

### Author: ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)
#### Post date: [October 19, 2021, 10:14am UTC](https://es.discourse.group/t/hooks/1035/3 "2021-10-19T10:14:00Z")

</div>

I would recommend you take some thought into how the pieces interact (including with `async`/`await` as applicable) - I'm not sure what you're even trying to suggest here other than adding `useState` into the language somehow.
