Get up and running with Nhost and React
Get up and running with Nhost and React
Create Nhost Project
Create your project through the Nhost Dashboard.
Setup Database
Navigate to the SQL Editor of the database and run the following SQL to create a new table todos
.
Track this
is enabledCREATE TABLE todos (
id uuid NOT NULL DEFAULT gen_random_uuid(),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
user_id uuid NOT NULL,
contents text NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES auth.users(id) ON UPDATE cascade ON DELETE cascade
);
Create Todos Table
Configure the todos table permissions
To set permissions for the new todos
table, select the table, click on the ...
to open the actions dialog,
then click on Edit Permissions. Set the following permissions for the user
role:
Insert
- Set
Row insert permissions
toWithout any checks
- Select all columns except
user_id
onColumn insert permissions
- Add a new
Column preset
and setColumn Name
touser_id
andColumn Value
toX-Hasura-User-Id
- Save
- Set
Insert Permissions
Select
- Set
Row select permissions
toWith custom check
and fill in the following rule:- Set
Where
totodos.user_id
- Set the operator to
_eq
- Set the value to
X-Hasura-User-Id
- Set
- Select all columns except
user_id
onColumn select permissions
- Save
- Set
Select Permissions
Update
- Set
Row update permissions
toWith custom check
and fill in the following rule:- Set
Where
totodos.user_id
- Set the operator to
_eq
- Set the value to
X-Hasura-User-Id
- Set
- Select all columns except
user_id
onColumn select permissions
- Save
- Set
Update permissions
Delete
- Set
Row delete permissions
toWith custom check
and fill in the following rule:- Set
Where
totodos.user_id
- Set the operator to
_eq
- Set the value to
X-Hasura-User-Id
- Set
- Save
- Set
Delete permissions
Configure permissions to enable user file uploads
To enable file uploads by users, set the permissions as follows:
-
Edit the files table permissions
- Navigate to the files table within the Database tab
- Click on the three dots (…) next to the files table
- Click on Edit Permissions
-
Modify the
Insert
permission for theuser
role:- Set
Row insert permissions
toWithout any checks
- Select all columns on
Column insert permissions
- Save
- Set
Insert Permissions
Select
- Set
Row select permissions
toWith custom check
and fill in the following rule:- Set
Where
tofiles.uploaded_by_user_id
- Set the operator to
_eq
- Set the value to
X-Hasura-User-Id
- Set
- Select all columns on
Column select permissions
- Save
- Set
Select permissions
Bootstrap your React app
Intialize a new React project using the template @nhost/react-apollo
npx create-react-app myapp --template @nhost/react-apollo
Connect your React app to the Nhost project
Copy your project’s <subdomain>
and <region>
values available on the dashboard overview
const nhost = new NhostClient({
subdomain: "<subdomain>", // replace the subdomain value e.g. "hjcuuqweqwezolpolrep"
region: "<region>", // replace the region value e.g. "eu-central-1"
});
Create the Todos Page and Add It to the Sidebar Navigation
Run the project
Run your project with npm start
and enter http://localhost:3000
in your browser.