2018-12-05 12:08:47 +01:00
---
2018-12-05 15:01:07 +01:00
title: React App Setup
2018-12-06 14:43:27 +01:00
description: This tutorial shows how you can build a basic [React ](https://reactjs.org/ ) app with [Create React App ](https://github.com/facebook/create-react-app ) that uses the squid-js JavaScript package to publish a data set, get a data set, and more.
2018-12-05 12:08:47 +01:00
---
2018-12-05 15:01:07 +01:00
## Requirements
2018-12-06 14:43:27 +01:00
- `Node.js` >= 10 is installed. You can check using `node -v`
2018-12-05 17:48:15 +01:00
- `npm` >= 5.2 is installed. You can check using `npm -v`
2019-04-17 14:17:36 +02:00
- `Spree` local Ocean test network
2019-04-04 11:56:56 +02:00
2019-04-16 19:19:53 +02:00
- Git clone the [oceanprotocol/barge ](https://github.com/oceanprotocol/barge ) repository, then in that directory:
2019-04-02 13:18:59 +02:00
- (Optional but recommended) Clean out all your old Docker stuff using `docker system prune --all --volumes`
2019-04-17 16:48:37 +02:00
- Use the startup script in Barge to run a local Spree Testnet:
2019-03-19 11:34:52 +01:00
```bash
2019-04-17 16:48:37 +02:00
export KEEPER_VERSION=v0.9.1 & & \
export AQUARIUS_VERSION=v0.2.2 & & \
export BRIZO_VERSION=v0.3.5 & & \
2019-03-19 11:34:52 +01:00
./start_ocean.sh --latest --no-pleuston --local-spree-node
```
2019-04-17 14:17:36 +02:00
- A Web3 capable browser, like Firefox/Chrome with MetaMask installed
- [Some Spree Ether ](/tutorials/get-ether-and-ocean-tokens/#get-ether-for-a-local-spree-testnet ) in your MetaMask account
2018-12-05 15:01:07 +01:00
2019-04-17 16:48:37 +02:00
Note that compiling and deploying the contracts in your local Docker network takes some time so it can take a few minutes until the network is ready to be interacted with. That usually is the case once `keeper-contracts_1` container doesn't show any messages anymore.
2018-12-06 16:38:22 +01:00
## New Create React App
2019-03-19 11:34:52 +01:00
First, kickstart your new React app by creating a boilerplate with Create React App:
2018-12-06 16:38:22 +01:00
```bash
npx create-react-app marketplace
```
2019-04-17 16:48:37 +02:00
This will create a folder named `marketplace` with a boilerplate React app. Go into that new folder and add the [Ocean Protocol JavaScript library ](https://github.com/oceanprotocol/squid-js ) to the app's dependencies:
2018-12-06 16:38:22 +01:00
```bash
cd marketplace/
2019-04-17 16:18:48 +02:00
npm install @oceanprotocol/squid@0 .5.6
2018-12-06 16:38:22 +01:00
```
At this point you can already run `npm start` which starts the app in your browser at [localhost:3000 ](http://localhost:3000 ):
![React App 01 ](images/react-app-01.png )
## Add Markup & Web3
Let's make it ours, open `src/App.js` and replace the whole source with:
```jsx
2019-04-17 14:35:48 +02:00
// src/App.js
2018-12-06 16:38:22 +01:00
import React, { Component } from 'react'
import './App.css'
class App extends Component {
render() {
return (
< div className = "App App-header" >
< h1 > Marketplace app< / h1 >
< / div >
)
}
}
export default App
```
Below the `import './App.css'` line, let's import the packages we installed, set up web3 and unlock MetaMask accounts (if locked):
```js
2019-04-17 14:35:48 +02:00
// src/App.js
2018-12-06 16:38:22 +01:00
import { Ocean } from '@oceanprotocol/squid'
import * as Web3 from 'web3'
const web3 = new Web3(window.web3.currentProvider)
window.ethereum.enable()
```
After those steps you should see this, and MetaMask should have asked you to allow access to your account:
![React App 02 ](images/react-app-02.png )
![React App 03 ](images/react-app-03.png )
2019-03-19 11:34:52 +01:00
Note: If you see an error like `inpage.js:1 MetaMask - RPC Error: Internal JSON-RPC error.` in your `console.log` , don't worry about it. It's a MetaMask thing.
2019-04-02 13:18:59 +02:00
## Create Ocean Instance
2018-12-06 16:38:22 +01:00
2019-03-19 11:34:52 +01:00
Now that we are successfully connected with Web3, we can set up our Ocean instance.
2018-12-06 16:38:22 +01:00
2019-04-02 13:18:59 +02:00
At the beginning of your component (i.e. right after the `class App extends Component {` line), create a new Ocean instance with all configuration within the `componentDidMount` lifecycle method. All Ocean Protocol operations can be executed from this Ocean instance.
2018-12-06 16:38:22 +01:00
```js
2019-04-17 14:35:48 +02:00
// src/App.js
2019-04-17 15:37:28 +02:00
//...
2018-12-06 16:38:22 +01:00
async componentDidMount() {
this.ocean = await new Ocean.getInstance({
web3Provider: web3,
2019-04-17 14:17:36 +02:00
nodeUri: 'http://localhost:8545',
aquariusUri: 'http://localhost:5000',
brizoUri: 'http://localhost:8030',
brizoAddress: '0x00bd138abd70e2f00903268f3db08f2d25677c9e',
parityUri: 'http://localhost:8545',
secretStoreUri: 'http://localhost:12001'
2018-12-06 16:38:22 +01:00
})
2019-04-17 15:37:28 +02:00
console.log('Finished loading contracts.')
2018-12-06 16:38:22 +01:00
}
2019-04-17 15:37:28 +02:00
//...
2018-12-06 16:38:22 +01:00
```
2018-12-06 14:43:27 +01:00
## Final Result
2018-12-05 15:01:07 +01:00
2018-12-06 16:38:22 +01:00
That's it, if you have no errors in your `console.log` then you have successfully initialized an Ocean instance in your brand new React app and you are ready for the [next part of this tutorial ](/tutorials/react-publish-data-set/ ).
![React App 04 ](images/react-app-04.png )
2018-12-05 12:08:47 +01:00
2018-12-05 15:01:07 +01:00
Here is the full source of `src/App.js` that you should have if you followed this tutorial:
2018-12-05 12:08:47 +01:00
2018-12-06 14:43:27 +01:00
```jsx
2019-04-17 14:35:48 +02:00
// src/App.js
2018-12-05 15:01:07 +01:00
import React, { Component } from 'react'
import './App.css'
2018-12-06 10:58:47 +01:00
import { Ocean } from '@oceanprotocol/squid'
2018-12-05 15:01:07 +01:00
import * as Web3 from 'web3'
2018-12-06 13:03:22 +01:00
2018-12-05 15:01:07 +01:00
const web3 = new Web3(window.web3.currentProvider)
window.ethereum.enable()
2018-12-06 13:03:22 +01:00
2018-12-05 15:01:07 +01:00
class App extends Component {
async componentDidMount() {
this.ocean = await new Ocean.getInstance({
web3Provider: web3,
nodeUri: 'http://localhost:8545',
aquariusUri: 'http://localhost:5000',
brizoUri: 'http://localhost:8030',
2019-04-03 14:40:15 +02:00
brizoAddress: '0x00bd138abd70e2f00903268f3db08f2d25677c9e',
2018-12-05 15:01:07 +01:00
parityUri: 'http://localhost:8545',
2019-04-03 14:40:15 +02:00
secretStoreUri: 'http://localhost:12001'
2018-12-05 15:01:07 +01:00
})
2019-04-16 18:14:00 +02:00
console.log('Finished loading contracts.')
2018-12-05 15:01:07 +01:00
}
2018-12-06 14:43:27 +01:00
2018-12-05 15:01:07 +01:00
render() {
return (
2018-12-06 16:38:22 +01:00
< div className = "App App-header" >
2018-12-05 17:48:15 +01:00
< h1 > Marketplace app< / h1 >
2018-12-05 15:01:07 +01:00
< / div >
)
}
}
2018-12-06 14:43:27 +01:00
2018-12-05 15:01:07 +01:00
export default App
```
2018-12-06 16:38:22 +01:00
Move on to [Publish a Data Set ](/tutorials/react-publish-data-set/ ).