hooks

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

proposal: hooks


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();

}


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);

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.

2 Likes

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.