How to create a plant Id feature with React
Tags: javascript, react, plantid
Published: 2020-08-31 02:11:12

Hello! Today I will be talking about how to add a plant Id feature to your app! Last week I was tasked with adding one of these to an existing project so I decided to share some of the things I learned along the way!

So you may be wondering what exactly does plant Id feature mean hehe! What I mean by this is creating a component that allows the user to submit a photo of a plant and then receive info back like the common name, scientific name, and a wikipedia link. This is done via the [Plant.id API](https://web.plant.id/plant-identification-api/). This API is really rad! It uses machine learning to make matches with its database based off of the data it receives. Cool stuff!

Before I combined this feature into the already existing project, I made a simple practice app to make sure I could access the API and receive the data with all the complexity of the existing app removed. To do this I used the convenient create react app package. I ran npx create-react-app command followed by the name of the project, in this case 'plantiddemo'. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bunpax0bpwd8n1o4yqvm.png)


Now we should have a file structure that looks like this: 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9b3n69egqp8g0e65fuaf.png)

We won't need most of these files but this is what running create react app sets you up with. Now if we cd into our plantiddemo directory and then run npm start we should see this in the browser:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rvkxnxk0d0pwidce44mc.png)

Next I'm going to change up the App file so it will just render a blank page.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/20w1b66vi6qn7o9ft2w1.png)

Now I need to create a PlantId component which will contain the functionality for the plant id feature and have App.js render it.
First I'm going to create the PlantId component. So I create a new file in my src directory called PlantId.js that contains a simple functional component that renders the text 'PlantId'. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jixqz77hzaojhd6bhg8d.png)

I made sure to export it because I need to use it in App.js so I can render it. The next step is to import it into App.js and add it to the return section. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/02s0bph0powbd19v3ks7.png)

Now when I go to my browser I see the text PlantId on the screen because my PlantId component contains that text. The next step is installing the [react file base64](https://github.com/BosNaufal/react-file-base64) package from npm like so:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m9hgszt7r7pwtbugmnxs.png)

This npm package allows us to convert a file from our hard drive into the base64 format. The Plant.id API only accepts files in this format and I believe that has something to do with the machine learning aspect of their API. Then I imported into into my PlantId component so I could use it.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vucjexb6bmg6vn9icq89.png)

Next I added the FileBase64 component to the return section of the PlantId component. Now my browser renders a component which allows me to upload a file from my hard drive.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vyxjzdgltgjsvodi1fsh.png)

The 'handleInput' part refers to the clickHandler that I'm about to create. After that I set up some state that will contain the uploaded photo and imported the useState hook from react. Then I created a handleInput function which updates the state once the file has been uploaded. So now my PlantId component look like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kdcavsxq3m1n0fyi8r6a.png)

Now my state has been updated all I have left to do is make the API call to [Plant.id](https://web.plant.id/) with the new state. Then I can do whatever I want with the data I get back! Before we send the info off to the API we need to add a .base64 to our plantFile state and slice of the first 23 characters because the data we get back from uploading our file looks like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8s3kuxqfb99ydnq2xeip.png)

But Plant.id needs it to look like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/84mbigioagi6l857uvgn.png)

Here is what the API call would look like:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zt13wz2sq5qf54ml2f4z.png)

I also need to make a button with a clickHandler to make the API call.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yibo75zd8x2yu5j8jril.png)

So that's how you can make use of the Plant.id API and identify a plant with a photo! I hope you enjoyed this walk through and thanks for reading!