JULIERAJ / KIDS-FIRST-v2

Second version of the app.
6 stars 4 forks source link

Refactoring setState Usage as a Prop #110

Closed AnnaSolovykh closed 3 months ago

AnnaSolovykh commented 3 months ago

Description
Refactor instances where setState is used as a prop in the codebase to ensure best practices and maintain code quality.

Context
Using setState as a prop can lead to various issues, including unintended side effects, difficulty in debugging, and poor component encapsulation. The goal is to locate all instances where setState is passed as a prop and refactor the code to avoid this pattern.

Requirements

Steps to Resolve

  1. Search for Usage:

    • If using Visual Studio Code, open the search panel (Ctrl+Shift+F) and enter the search query set[A-Z][a-zA-Z]*\s*=\{ to find all occurrences. Click on the .* icon to the right of the search input box to enable regex search. Image
  2. Refactor Example:

    • Before:
      <ChildComponent setState={setState} />
    • After:
      // Pass a callback function instead
      <ChildComponent updateState={(newState) => setState(newState)} />
  3. Test Changes: Ensure all refactored components are tested to verify that state updates work correctly without passing setState as a prop.

Additional Resources

Nassimancer commented 3 months ago

After reviewing the code in the following files: ResetPassword.jsx, Signin.jsx, RegisterForm.jsx, Register.jsx, EmailVerify.jsx, ForgetPassword.jsx, components do not use setState or any state update function as a prop. The code adheres to the best practice of avoiding passing setState functions as props, ensuring proper encapsulation and maintainability of the component.

AnnaSolovykh commented 3 months ago

These are those I could find now quickly. Please check the whole codebase to ensure you find all of them. setState - is a general name because it is used for setting the state. Other namings depending on the purpose are used in the codebase. Image Image Image

@Nassimancer

AnnaSolovykh commented 3 months ago

Let's change the strategy of searching, @Nassimancer ! We will use regex for that. I have updated the instructions.

Please make sure to the the resources I attached about lifting the state. You can, of course, use some others too, first look into it in details.

AnnaSolovykh commented 3 months ago

https://www.robinwieruch.de/react-lift-state/ https://www.robinwieruch.de/react-derive-state-props/

I will add up more links.