{"id":21106917,"url":"https://github.com/buildo/eslint-plugin-class-prefer-methods","last_synced_at":"2025-07-08T16:31:24.124Z","repository":{"id":57229757,"uuid":"98088083","full_name":"buildo/eslint-plugin-class-prefer-methods","owner":"buildo","description":"eslint plugin to report not-needed usage of arrow functions instead of class methods","archived":false,"fork":false,"pushed_at":"2018-07-04T09:45:14.000Z","size":11,"stargazers_count":3,"open_issues_count":2,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-08-11T00:48:20.700Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/buildo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-23T09:38:53.000Z","updated_at":"2018-07-04T09:45:16.000Z","dependencies_parsed_at":"2022-09-14T14:50:35.678Z","dependency_job_id":null,"html_url":"https://github.com/buildo/eslint-plugin-class-prefer-methods","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buildo%2Feslint-plugin-class-prefer-methods","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buildo%2Feslint-plugin-class-prefer-methods/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buildo%2Feslint-plugin-class-prefer-methods/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buildo%2Feslint-plugin-class-prefer-methods/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/buildo","download_url":"https://codeload.github.com/buildo/eslint-plugin-class-prefer-methods/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225018864,"owners_count":17407947,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T00:34:20.916Z","updated_at":"2024-11-20T00:34:21.531Z","avatar_url":"https://github.com/buildo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# eslint-plugin-class-prefer-methods \nEslint plugin to report not-needed usage of arrow functions instead of methods in a React Component class.\nIt also throws an error if you pass a class method as callback to some `children`\n\n## Install\n```sh\nnpm i --save-dev eslint-plugin-class-prefer-methods\n```\n\n## Usage\nIn your `.eslintrc`:\n\n```javascript\n{\n  \"plugins\": [\n    \"class-prefer-methods\"\n  ],\n  \"rules\": {\n    \"class-prefer-methods/prefer-methods\": 2\n  }\n}\n```\n\n## Why\nArrow function properties are automatically binded to `this`.\nThis is really handy when you need to pass it as a callback to a `children`, but it's not necessary in most cases and it impacts performances: the RAM occupied by the component and the JS time to instantiate it are both higher.\n\n## Examples\nExample of incorrect code for `\"class-prefer-methods/prefer-methods\": 2` rule:\n\n```jsx\nclass Example extends React.Component {\n\n  state = { clicked: false }\n\n  // this could safely be a class method \n  shouldRender = () =\u003e {\n    return this.props.shouldRender === true;\n  }\n  \n  // this should be an arrow-function property as it's passed as callback to a children\n  onClick() {\n    this.setState({ clicked: true })\n  }\n  \n  render() {\n    if (!this.shouldRender()) {\n      return null;\n    }\n    \n    return \u003cdiv onClick={this.onClick} /\u003e;\n  }\n\n}\n```\n\nExample of correct code for `\"class-prefer-methods/prefer-methods\": 2` rule:\n\n```jsx\nclass Example extends React.Component {\n\n  state = { clicked: false }\n\n  // class methods are lighter\n  shouldRender() {\n    return this.props.shouldRender === true;\n  }\n  \n  // callbacks that use \"this\" must be arrow-functions to work properly\n  onClick = () =\u003e {\n    this.setState({ clicked: true })\n  }\n  \n  render() {\n    if (!this.shouldRender()) {\n      return null;\n    }\n    \n    return \u003cdiv onClick={this.onClick} /\u003e;\n  }\n\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuildo%2Feslint-plugin-class-prefer-methods","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbuildo%2Feslint-plugin-class-prefer-methods","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuildo%2Feslint-plugin-class-prefer-methods/lists"}