Quick Start
In this section you will see how to install and set up React Launchkit in your application. If you used the CLI to create your project, you should already have React Launchkit installed and set up.
This quickstart assumes you have a React application set up with Vite and that your backend is built with Strapi.
Installation
- npm
- yarn
npm install react-launchkit
yarn add react-launchkit
Usage
Set up your API axios instance
We create an axios instance wrapped with the Launchkit interceptor. This allows us to handle authentication and authorization in our application.
You can use this instance all over your application to make API requests.
First let's install axios:
- npm
- yarn
npm install axios
yarn add axios
Then create the following file:
import axios from "axios";
import { setupInterceptors } from "react-launchkit";
export const api = setupInterceptors(
axios.create({
baseURL: import.meta.env.BASE_URL,
})
);
Set up your config
Create a config file to store your API URL and other configurations.
import { LaunchKitConfig, Login, Register } from "react-launchkit";
import { api } from "../services/api";
export const config: LaunchKitConfig = {
api,
auth: {
usersMePath: '/api/users/me',
logoutRedirect: '/login',
},
};
Set up the LaunchKitProvider and Router
Now that your API instance and config are set up, you can use the LaunchKitProvider
and Router
components to set up your application.
// 1. Import the provider and router
import { LaunchKitProvider, Router } from "react-launchkit";
// 2. Import your config
import { config } from "./config/launchkit";
function App() {
return (
// 3. Wrap your application with the provider and pass the config
<LaunchKitProvider config={config}>
// 4. Add the Router component with the routes
<Router
routes={[
{
path: "/",
component: <div>Home</div>,
access: "public",
},
{
path: "/login",
component: <div>Login</div>,
access: "public",
},
]}
/>
</LaunchKitProvider>
);
}
export default App;
That's it! You have successfully set up React Launchkit in your application.