To improve the user experience of our app, we want to add tabs on the main page. One tab will show all pending tasks, while the other one will show the completed tasks.
It is possible to add nested routes in BlueBase. Whenever we want to do this, we will add a navigator
property in the route. There are several navigators available to use (i.e. switch
, stack
, tab
, bottom-tabs
, drawer
).
Inside the navigator, we add our desired routes:
When you run the app, you should now see 2 tabs on the main page like the screenshots below:
Another thing we can do to make our UI pleasing is by adding icons to our tabs. This is done by:
Adding tabBarIcon
to route.options
.
Adding tabBarOptions
to the navigator
.
See the code below for example:
Ok, so now with the configurations out of the way, it's time to start coding our app. The first thing we need to do is to create an application skeleton by developing empty screens and configuring the navigation mechanism.
The first screen that we are going to create is going to show a list of pending tasks.
Let's start by creating our screen view component. Let's call it PendingTasksScreen
. Feel free to copy the code below.
All we're doing right now is rendering the component name. Don't worry we will add the actual list later.
Also, create the following index file:
And also:
BlueBase provides an abstraction to create navigation plugins. This allows us to be able to use any navigation library in our app. For now, we have created React Navigation & React Router integrations.
The navigation API is loosely based on React Navigation V5. It is very simple to create your own routes. Just add the routes
property to your plugin. This is an array of objects:
Explanation of the code above:
Line 12: We changed the plugin's index route to the new one created at Line 23. By doing this, whenever we press the task icon on our launcher, we get navigated to this screen.
Line 19: Notice we added the PendingTasksScreen
component to BlueBase. This is so we can reference it in our route configuration at Line 24. This is where we tell BlueBase what component to render once we have navigated to that route.
Line 23: Name of the route. We use this name to navigate to this screen. We will see an example of this later.
Line 25: Path of the screen. This is used to create the URL for the web. The URL pattern is [[host]]/p/[[plugin-key]]/[[route-path]]
. Since this is the plugin home page, we leave it empty. Please see the URL in the browser screenshot for an example.
Line 28: Route options, like title, etc.
That's all. Run your app, and test your new screen.
We also need a screen to create a new task.
Similar to the previous section, now we create our third route. Notice that I have now moved the routes array dedicated file, to keep to code readable.
Import this array to your plugin file:
This should create the third route, which should be accessible via the following URL:
But we still need a way to navigate to this route in our UI. This time we will add an icon button in the header.
Create the following component:
Also, create an index file to export the component:
To add our new TaskCreateButton
to the screen, add the headerRight
property in the route options like so:
That's it, run your app to test it out.
Next up, we are going to create a screen that will allow a user to edit a task.
Before we move forward to adding new routes, let's quickly create 3 copies of our PendingTasksScreen
component, and give them the following names:
CreateTaskScreen
EditTaskScreen
CompletedTasksScreen
So now we should have 4 screens in total.
Add the following EditTask
route to the routes
array:
This should create the second route, which should be accessible via the following URL:
But we still need a way to navigate to this route in our UI. Let's modify our PendingTasksScreen
component to add a button:
Notice, that we use the push
method from BlueBase's useNavigation
hook. The method takes route name and optional params object as its arguments.
Run your app, you should now be able to navigate between routes.
Notice that the value of path property in the route is :taskId
. This means that any value that is entered in this part of the URL will be captured in the taskId
param. For example the URL http://localhost:19006/p/tasks/123
will extract value 123
in the taskId
variable.
Let's see how we can use this value, modify the EditTaskScreen
component to match the following content:
Notice the code on Line 7, we use the getParam
function to extract the URL param.