























Building user interfaces works best by building small components and then combining and reusing them across your application. This ensures that different components in your application are not tightly coupled and encourages reusability. React.js, or React, is a free and open source JavaScript library that can help you do this.
However, creating a new page in React can be difficult, especially if you have a complex view with multiple components that must be rendered based on various conditions. This is where React-Bootstrap comes in.
React-Bootstrap is a frontend library providing an array of components and many other functionalities out of the box that you can directly import into your website instead of building them from scratch. One such component is bootstrap forms that you can use to easily create and design complex forms in React.
In this article, you'll learn about React-Bootstrap by creating a complete login and sign-up flow with bootstrap forms; you'll then see how to use tools like Clerk to further ease your life in terms of managing authentication and authorization flow.
Let's start with React-Bootstrap; the next section will talk about what React-Bootstrap forms are and how they can help you increase the quality of your application while saving you time and effort.
React-Bootstrap is a frontend component library that provides pure React components. React-Bootstrap includes a plethora of ready-to-use components that you can import directly into your application. It's also very simple to make these components responsive, which would otherwise be a very time-consuming task.
The documentation for React-Bootstrap is also very well supported and simple to understand, allowing you to get started with a lower learning curve.
There are several scenarios in which you might want to create forms for your website. Perhaps you need a sign-in/sign-up page for your customers to log in, an address page for an e-commerce app, or have some other use case where you want multiple inputs from your app's user. Even something as simple as adding a task to your to-do app requires a form.
Creating complex workflows in forms can be difficult. You must have an array of input types, such as text, password, and so on, as well as various validations based on input types and state attributes. This can eventually lead to hundreds of lines of code that are difficult to understand and debug.
This is where React-Bootstrap forms shine. With bootstrap forms, you can create a form with minimal code.
In the following sections, you'll learn how to design and build a sign-in/sign-up page using bootstrap forms. Let's get started!
In this section, you'll first see how to create a React project and then use React-Bootstrap to create your own sign-in/sign-up page.
Before you can start writing code, you must first install React, React-Bootstrap, and other dependencies in your project. If you already have these installed, you can skip this section.
Run the commands node —version and npm —version to see if Node.js and npm are already installed. If it says command not found for either command, proceed to the next step.
The first thing you need to do is download a Node.js installer for your system. As recommended, download the LTS version. Install and run the software. This will automatically install Node.js and npm for you.
Once you're done installing, run the above commands once more to verify that both npm and Node.js have been correctly installed.
Once you have npm installed, it's easy to create a React project. Just run the command npx create-react-app <projectname>. This example will use "bootstraplogin" as the project name.
Once the project is created, you need to install React-Bootstrap. For that, first run cd bootstraplogin and then run npm install react-bootstrap bootstrap. This will install React-Bootstrap and bootstrap to your project and add them as dependencies in your package.json file.
To ensure that everything is working as it should, run the command npm start and visit http://localhost:3000 to ensure that the React default page is getting loaded like this.

If this page is loading correctly, your setup is complete.
In the section below, you'll get to grips with form creation using React-Bootstrap. You'll create and style a basic form in which users are asked to provide their email and password in order to sign in.
Before you dive into implementing your login flow, let's get the basics of bootstrap forms right.
Initially, your App.js file, which you can find in the src folder of the project, will look something like this:
You will now update your App.js to create a basic form page as described below.
The first thing you need to do is import the React-Bootstrap CSS. For that, add import 'bootstrap/dist/css/bootstrap.min.css'; to your index.js file.
Once that's done, update your App.js file to use bootstrap forms as below:
The UI should now look like this.

The first thing to notice in the above example is that you're importing Form from react-bootstrap/Form.
<Form> is the top-level API provided by React-Bootstrap, which wraps all the functionality required to build forms.
The <Form.Control> component renders the input component with bootstrap styling. Forms support different types of inputs like plaintext, email, password, file, or even color as input.
After that, you have <Form.Group>, which creates a section inside the form view by wrapping the <Form.Control> with its own styling and validations.
There's also <Form.Label>, which works as the title for the form group, and <Form.Text>, where you can add additional text.
React-Bootstrap provides two main features for users to easily build responsive and well-designed web pages.
The grid system: Bootstrap's grid system uses a series of containers, rows, and columns to lay out and align content. It's built with Flexbox and is fully responsive. The grid also offers the following:

className to your component.For example, <div className="mb-3 mt-4"/> provides a bottom margin of 3 pixels and a top margin of 4 pixels for the div.
Now that you've covered the fundamentals, let's create a sign-in/sign-up page for your website. You can find the entire code in this GitHub repo.
The first thing to do is create the SignIn.js file in your src folder and add the below code:
Now, let's see exactly what's happening in the above example.
<Container> to wrap the component. Containers provide an easy means to center and horizontally pad the content you put inside it.<Row> and a <Col> element. The former is used to add CSS to the component to orient the entire screen horizontally and align the internal component to center; the latter is used to add breakpoints to ensure a responsive view across any screen size.<div> adds a blue horizontal line to the view for which the div is styled using the border utility provided by React-Bootstrap.<Card> and <Card.Body>. Card is a basic container with inherent styling like a rounded border and some padding. Inside the card, you can have a header, footer, and body. In the example, you only use Card.Body.<div> with className values as mb-3 and mt-4, which provide a bottom margin of 3 pixels and a top margin of 4 pixels to the div.<h2> tag for the heading in the view. fw-bold sets font weight as bold, text-uppercase makes the text uppercase, while mb-2 is again setting 2 pixels as the bottom margin.<p> tag to add text.<Form>.<Form>: one for the username and the other for the password using <Form.Group>. ControlId here works as a unique id for the FormGroup.<Form.Label> to add a heading for the input.<Form.Control> converts to the actual HTML input element in the view. type is given as email, which checks for a valid email address, and placeholder is added, which will be shown when the user has not yet provided input.type as Password for Form.Control also ensures that the input is not visible but is added as a **.<div>, wrapping a clickable link and a p tag inside it; the p tag has className "small" and "text-primary", which sets size and color for the inside text respectively.# with that link.<div> wrapping a clickable button. If you want to find out more, React-Bootstrap provides an API for buttons.<div> with a link that once clicked should open a sign-up page for the user.Your sign-in page should look like below.

The sign-up page has similar components to the sign-in page but requires some extra details, which you'll see below.
First, you need to create the SignUp.js file in your src folder and add the below code:
Let's see what each line of code does.
<Card>, including <Card.Body>, which captures the header text, and then the <Form>.<Row>, which was not the case in the sign-in page. Row has two <Form.Group>s: one for a name and another for a phone number. This is so that these two form groups can be rendered in a single row. Along with adding <Row>, you also have to add as={Col} to the<Form.Group> to align these two form groups in a single row.<Row>, which includes two form groups. These form groups are for users to add their username and password.<InputGroup> is added. InputGroup easily extends the Form.Control component by adding text, buttons, or button groups on either side of the input.@brand.com is added as <InputGroup> text, which signifies that the email address for the user will be "".
You saw above how easy it is to create your own sign-in and sign-up page using Bootstrap; however, there are aspects that Bootstrap doesn't automatically take care of, which are essential in creating a secure and functional website. For example, along with asking the user to upload their information, you also need to store that information in your database and validate the username and password when the user tries to log in.
There are various other features like authorization, social logins, and multifactor authentication, which are very complex workflows in themselves where you must ensure that your system's security is never compromised. This means that as the product owner, you must keep up with advancements to ensure a secure product.
In light of these considerations, it might be a good idea for you to delegate the sign-in/sign-up functionality to a SaaS platform like Clerk that takes care of all these complex tasks for you.
Clerk is a tool that makes it simple for you to integrate authentication and authorization functionality with your web and mobile apps without having to worry about low-level security details or even the design of the sign-in/sign-up page.
Before we get into integrating Clerk into your application, you'll have to create an account. Sign up with Clerk and you'll be shown the below page.

Let's go section by section. First, you need your application name. You can add more applications later, so make sure the name is specific to the project you're working on.
After that, you need to specify how you want to authenticate your users. You can choose multiple options based on your requirements. You can also mention the social media platforms you want to use for authentication.
Once you're done, click Submit and you'll be directed to your application page. Just click Try it now and it will redirect you to your sign-in/sign-up page.
Integrating Clerk with your React application is a simple process. All you have to do is follow the instructions in this article. Let's set it up step by step below.
cd <path_to_your_project>.npm install @clerk/clerk-react.env variables using the touch .env.local command.echo "REACT_APP_CLERK_FRONTEND_API=clerk.sharp.mustang-37.lcl.dev" > .env.local. This will add REACT_APP_CLERK_FRONTEND_API as an env variable in your file.And that's all you need to integrate with Clerk. It's fairly easy, right? Now let's go and make the required code changes to integrate your application with Clerk.
Add the below code to the App.js file in your src folder:
Now let's see what the above code is doing:
frontendApi tells the Clerk framework which API endpoint to hit to get the user-related information.<ClerkProvider> component wraps your React application to provide active session and user context. It must always be added to the entrypoint of your React application.<SignedIn> component offers authentication checks for your application. Any child components wrapped by a SignedIn component will be rendered only if there's a user signed into an active session in your application. For example, when you want the cart and profile page to be visible only to logged-in users, you can use <SignedIn>.<SignedOut> component will be rendered only if the user is not yet signed in to your application.Hello component, the useUser hook gets the information regarding the logged-in user. The <UserButton> provides you with a clickable button.<Hello /> component, which shows user information and a <UserButton>.<RedirectToSignIn>.The signed-out view looks like the following.

And here's the signed-in view.

Now that you understand how Clerk works and what features it provides, let's discuss the benefits it offers over implementing your own login flow using React-Bootstrap.
In this article, you learned how to create your sign-in and sign-up page, first using React-Bootstrap and then recreating it using Clerk. You also learned various benefits of React-Bootstrap and Clerk and how the latter enables you to completely offload the complex authentication and authorization flow.
Clerk helps you in building your sign-in and sign-up page just with a few clicks. It provides an array of functionalities like social sign in, SSO, multifactor authentication, user information, session management, etc. By doing so, it helps you focus on solving your business problems without worrying about the security of your application.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。