{"id":25169729,"url":"https://github.com/sofiane-abou-abderrahim/react-class-based-components","last_synced_at":"2026-04-15T15:42:02.915Z","repository":{"id":233000180,"uuid":"785689120","full_name":"sofiane-abou-abderrahim/react-class-based-components","owner":"sofiane-abou-abderrahim","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-14T11:31:03.000Z","size":342,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-14T11:45:00.397Z","etag":null,"topics":["class-based-components","error-boundaries","events","javascript","lifecycle-methods","react","reactjs","state-management"],"latest_commit_sha":null,"homepage":"https://sofiane-abou-abderrahim.github.io/react-class-based-components/","language":"JavaScript","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/sofiane-abou-abderrahim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-12T12:14:27.000Z","updated_at":"2024-04-14T11:09:30.000Z","dependencies_parsed_at":"2024-04-19T01:46:54.713Z","dependency_job_id":null,"html_url":"https://github.com/sofiane-abou-abderrahim/react-class-based-components","commit_stats":null,"previous_names":["sofiane-abou-abderrahim/react-class-based-components"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofiane-abou-abderrahim%2Freact-class-based-components","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofiane-abou-abderrahim%2Freact-class-based-components/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofiane-abou-abderrahim%2Freact-class-based-components/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofiane-abou-abderrahim%2Freact-class-based-components/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sofiane-abou-abderrahim","download_url":"https://codeload.github.com/sofiane-abou-abderrahim/react-class-based-components/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247071895,"owners_count":20878785,"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":["class-based-components","error-boundaries","events","javascript","lifecycle-methods","react","reactjs","state-management"],"created_at":"2025-02-09T08:36:24.108Z","updated_at":"2026-04-15T15:41:57.876Z","avatar_url":"https://github.com/sofiane-abou-abderrahim.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Class-Based Components\n\n## An Alternative Way Of Building Components\n\n- What \u0026 Why?\n- Working With Class-based Components\n- Error Boundaries\n\n# Steps\n\n## 0. Starting Project\n\n1. run `npm install`\n2. run `npm start`\n3. create `README.md`\n\n## 1. Adding a First Class-based Component\n\n1. define a new class named `User` in `User.js`\n2. import `component` from React \u0026 extend it on the `User` class\n3. access the `props` property with help of the `this` keyword\n\n## 2. Working with State \u0026 Events\n\n1. convert the `Users.js` functional component into a class-based component\n   1. create a class named `Users`\n   2. add a method named `toggleUsersHandler` in this class\n   3. define state with help of the `constructor` method to initialize it\n   4. change your state by calling the special `setState` method\n2. render this state in the `render` method\n3. derive the `userList` inside the `render` method as well\n4. make the `this` keyword in the `toggleUsersHandler` function refers to the surrounding class by binding `this`\n5. call the `toggleUsersHandler` function in the `render` method\n6. call super constructor in derived class because when you add the `constructor` to your class and you extend another class, you need to call super which calls the constructor of the `super()` class so if the class were inheriting from (`Component`)\n\n## 3. The Component Lifecycle (Class-based Components Only!)\n\n1. add the `UserFinder.js` component\n2. get rid of the `DUMMY_USERS` \u0026 refer to `this.props.users` instead of it in `Users.js`\n3. output the `\u003cUserFinder\u003e` component instead of the `\u003cUsers\u003e` component in `App.js`\n4. add the `UserFinder.module.css` file\n\n## 4. Lifecycle Methods In Action\n\n1. convert the `UserFinder.js` component into a class-based component\n2. use the `componentDidUpdate()` lifecycle to handle side effects and replace `useEffect()`\n3. use `componentDidMount()` to simulate a case where the data comes from an http request\n4. use `componentWillUnmount()` in `User.js`\n\n## 5. Class-based Components \u0026 Context\n\n1. add a new `users-context.js`\n2. use context with the first approach: the context `Consumer` component by adding `\u003cUsersContext.Consumer\u003e` in `UserFinder.js`\n3. or use `static contextType` in `UserFinder.js`\n4. now access the context with `this.context.users` instead of `DUMMY_USERS`\n\n## 6. Introducing Error Boundaries\n\n1. simulate a server error by throwing an error in the `componentDidUpdate` lifecycle method in `Users.js`\n2. use `try` / `catch` to prevent the app from crashing\n3. if you throw an error in a component and want to handle it in a parent component, build an error boundary\n4. add a `ErrorBoundary.js` file\n5. build a class-based component named `ErrorBoundary`\n6. use the `componentDidCatch()` lifecycle method which is going to be triggered whenever a child component throws an error\n7. in `UserFinder.js`, wrap the `\u003cErrorBoundary\u003e` component around the `\u003cUsers\u003e` component\n8. cacth the error with `componentDidCatch()`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsofiane-abou-abderrahim%2Freact-class-based-components","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsofiane-abou-abderrahim%2Freact-class-based-components","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsofiane-abou-abderrahim%2Freact-class-based-components/lists"}