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

no-nested-object-setvalue throws when passing a function call #17

Closed
akd-io opened this issue Mar 14, 2022 · 2 comments · Fixed by #18
Closed

no-nested-object-setvalue throws when passing a function call #17

akd-io opened this issue Mar 14, 2022 · 2 comments · Fixed by #18
Assignees
Labels
bug Something isn't working

Comments

@akd-io
Copy link

akd-io commented Mar 14, 2022

Describe the bug
react-hook-form/no-nested-object-setvalue is thrown with message Avoid passing object or array as second argument in setValue since this is less performant, when using the following syntax:

setValue("someName", myFunctionReturningANumber())

To Reproduce

  1. The code you are going to lint.
function Component() {
  const {useValue} = useForm();

  const myFunctionReturningANumber = () => 5

  setValue("someName", myFunctionReturningANumber())

  return null;
}
  1. Your ESlint config
{
  ...
  'react-hook-form/no-nested-object-setvalue': ['error', { bracketAsArrayIndex: true }],
}

Expected behavior
Expect react-hook-form/no-nested-object-setvalue not to throw an error.

Actual behavior
react-hook-form/no-nested-object-setvalue throws with message Avoid passing object or array as second argument in setValue since this is less performant

Enviornment (please complete the following information):

  • Eslint version: 7.12.1
  • Node version: v14.18.3
  • React-Hook-Form version: 6.15.8
  • eslint-plugin-react-hook-form version: 0.2.3

Additional context
n/a

@andykao1213
Copy link
Owner

@akd-io Thanks for opening the issue. Currently, the rule will throw an error if the second argument of the setValue is not a Literal. This should also throw an error when we pass a variable to the setValue, which is not ideal.
To solve this problem, my proposal is only to throw errors if we directly pass arguments with the ObejctExpression or ArrayExpression. Other than those should be fine. But one should notice that if passing variable or execute function will not work:

const { setValue } = useForm()
// These will throw an error
setValue('field1', [....]) 
setValue('field2', {...})
// These won't throw error
const foo = () => []
setValue('field3', foo())
const arr = []
setValue('field3', arr)

Let me know if you have any concerns about this. Thank you.

@andykao1213
Copy link
Owner

Issue fixed in v0.2.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants