Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is returning the hook context from a hook required if hook object ref is unchanged? #1644

Closed
jnardone opened this issue Oct 24, 2019 · 4 comments

Comments

@jnardone
Copy link
Contributor

jnardone commented Oct 24, 2019

A hook generally follows the form

const myHook = hook => {
  // do stuff
  if (hook.type)...

  // optionally change stuff in hook
  hook.result = {...};

  return hook;  // <-- do I need to do this?  Even if I modified data inside 'hook'?
}

My question: if we don't create a new hook object but simply mutate the existing one, is return hook necessary? I don't think it is but I haven't seen it explicitly documented anywhere.

I do realize that if you

  ...
  // you can also change the entire hook object
  const newHook = Object.assign({}, hook);

  return newHook;  // you MUST do this

then you have to explicitly return the context.

Is there a preferred "you should always return the hook context from any hook that modifies hook data?" style or convention to follow?

@daffl
Copy link
Member

daffl commented Oct 24, 2019

It is not required, a hook can also return nothing (see Hooks API).

Returning a new context will probably be deprecated with the new hook system as discussed in #1443

@jnardone
Copy link
Contributor Author

ok, so if #1433 goes forward, there's no real use case for having to return the context since it'll be the one same object all the way through

@daffl
Copy link
Member

daffl commented Oct 24, 2019

It already is if your hook returns nothing. You can already safely remove return hook; from all your hooks.

@jnardone
Copy link
Contributor Author

Superb. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants