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

Universal React forms #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Universal React forms #8

wants to merge 4 commits into from

Conversation

jackfranklin
Copy link
Owner

A lot of people ask me how I would deal with forms in a Universal React world. When I say I'd just make it submit either on the server or in the client, they ask how to avoid writing it twice.

This PR shows a basic approach (there's plenty of room for improvement) where any logic around validating the form is in a universal module used on both the client and the server. You write:

  • A server only endpoint that the form will be POSTed to in a non-JS environment
  • A client only bit of code to capture the form submit and validate / deal with data locally
  • A shared module that deals with form logic such as validations.

Note that I won't merge this PR as I want to keep the basic repo as straightforward as it can be, but I hope this helps some people.

I'll blog more about this soon...


renderForm() {
return (
<form method="post" action="/submit-email-form" onSubmit={this.formSubmit}>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a non-JS world the form will submit like a regular form would to /submit-email-form, but in a JS world React will capture it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant