Code Monkey home page Code Monkey logo

phrg-react-hooks-extended-tutorial's Introduction

Extending Your CRUD Application

Now that we have a working CRUD application using React hooks, let's have some fun with it. Navigate back to your project and create a custom branch before starting.

Refactor

First, let's refactor some of the code in our AddUserForm.js and EditUserForm.js. Did you notice that the onSubmit prop in our <form>s are passed anonymous functions? This may look okay for short callbacks, like the one-liner for the onClick prop on some of the app's buttons. But it is better to separate more complex logic out of React's Virtual DOM and place it higher in the function definition.

Take the handleInputChange function as an example. Moving the logic of this callback into the body of the AddUserForm const not only DRYed the application, but it made the Virtual DOM easier to read.

In Nitro, ESLint prevents anonymous functions from getting passed to props in our React code. So let's not do that in this project either!

Replace the <form ... in AddUserForm and EditUserForm with <form onSubmit={handleSubmit}> and move its logic into a handleSubmit const. Use handleInputChange as an example.

Commit the work to your custom branch.

BONUS

Refactor all the anonymous functions passed to props out of the project.

New User Field

Let's add a new field to our user records. Every user will now have a name, a username, and a spiritAnimal. Here are some things to check as you make this addition:

  • Initial user records
  • Initial form state
  • The user table display
  • The new and edit forms

Commit the work to your custom branch.

Delete All Users

Let's add a feature that allows the users of our application to delete ALL the user's in the application. To accomplish this, add a button below the UserTable in the App.js like so:

<button
    className="button muted-button"
    onClick={deleteUsers}>
  {'Delete All Users'}
</button>

Then write a deleteUsers function to handle removing all of the user records.

When you are done, your Application should look something like this:

Spirit Animals

Commit the work to your custom branch.

Merge your work

Navigate to your repo on Github, open up a Pull Request for your custom branch, and merge it to master.

BONUS

Have an idea for another feature? Go for it!

Before you start, create another custom branch and commit often.

Once you finish, open up another Pull Request and show off your changes to the class.

phrg-react-hooks-extended-tutorial's People

Contributors

garettarrowood avatar

Watchers

 avatar  avatar

Forkers

lisswart

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.