React

Parse Data Types in a React component

Introduction

In the heart of Parse Core feature is the data objects management. Parse allows you to store and query its data straightforwardly using its SDKs or APIs (REST or GraphQL). All the data object features are built using the Parse.Object class, which fields may contain key-value pairs of several JSON-compatible data types. The primary data types that can be assigned to the object fields are the following:

  • Number: integer (42) or floating-point (42.5) numbers, as long as ‘.’ is the decimal separator;
  • boolean: true or false values;
  • string: a string that can be as long as 2147483647 characters. Be aware that values this huge will slow down data operations;
  • DateTime: DateTime objects stored in UTC format as default. If you need to use another timezone, conversion should be done manually;
  • array: an array containing data in any Parse compatible data.
  • object: a JSON object also containing any Parse data. When available in SDK, an include() call will bring details from the Object property.

When you choose to use the Array type, we recommend keeping array objects small as this can affect your data operations’ overall performance. Our recommendation is to use the Array type if it does not exceed 20 elements and does not grow over time. Instead of the Array type, you can use the Pointer and Relations types as an alternative.

In this guide, you will learn how to store data in each of the basic data types listed above. You will build a product registration component on React, which will show you how to format, convert and save data to your Parse Server in React.

Parse also offers the datatypes GeoPoint to use the power of geolocation resources, and the Parse-specific relational data using the types Pointer or Relation. You will see both covered in the next following guides.

Prerequisites

To complete this tutorial, you will need:

Goal

To understand the Parse-compatible basic data types, and to store each data type on Parse from a React component.

Step 1 - The Product Creation Component

Let’s first create the component structure. Let’s make it simple and create a form screen with one text input to each data type, one checkbox, and a submit button to save the object. These inputs will collect your Product field values: name (string), quantity (number), price (number), available (boolean), expiration date (DateTime), and categories(array). Also, you will save an additional object type field in your saving method as well, but this one won’t need an input field.

Create a separate component in a file called ProductCreation.js/ProductCreation.tsx including the following code, or add it to your main application file (App.js/App.tsx). You can use this layout with complete stylings using Ant Design and adding the CSS code to your App.css file or set up your own custom form.

ProductCreation.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React, { useState } from 'react';
import Parse from 'parse/dist/parse.min.js';
import './App.css';
import { Button, Checkbox, Input } from 'antd';
import { PlusOutlined } from '@ant-design/icons';

export const ProductCreation = () => {
  // State variables
  const [productName, setProductName] = useState('');
  const [productQuantity, setProductQuantity] = useState('');
  const [productPrice, setProductPrice] = useState('');
  const [productAvailable, setProductAvailable] = useState(false);
  const [productExpirationDate, setProductExpirationDate] = useState('');
  const [productCategories, setProductCategories] = useState('');

  return (
    <div>
      <div className="header">
        <img
          className="header_logo"
          alt="Back4App Logo"
          src={
            'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png'
          }
        />
        <p className="header_text_bold">{'React on Back4App'}</p>
        <p className="header_text">{'Product Creation'}</p>
      </div>
      <div className="container">
        {/* Product field inputs */}
        <div className="flex_between">
          <h2 className="list_heading">Available?</h2>
          <Checkbox
            onChange={(e) => setProductAvailable(e.target.checked)}
          ></Checkbox>
        </div>
        <div className="form_wrapper">
          <Input
            className="form_input"
            value={productName}
            onChange={(event) => setProductName(event.target.value)}
            placeholder="Name"
            size="large"
          />
          <Input
            className="form_input"
            value={productQuantity}
            onChange={(event) => setProductQuantity(event.target.value)}
            placeholder="Quantity"
            size="large"
          />
          <Input
            className="form_input"
            value={productPrice}
            onChange={(event) => setProductPrice(event.target.value)}
            placeholder="Price"
            size="large"
          />
          <Input
            className="form_input"
            value={productExpirationDate}
            onChange={(event) => setProductExpirationDate(event.target.value)}
            placeholder="Expiration Date (mm/dd/yyyy)"
            size="large"
          />
          <Input
            className="form_input"
            value={productCategories}
            onChange={(event) => setProductCategories(event.target.value)}
            placeholder="Categories (separated by comma)"
            size="large"
          />
          {/* Add product button */}
          <Button
            type="primary"
            className="form_button"
            color={'#208AEC'}
            size={'large'}
            onClick={createProduct}
            icon={<PlusOutlined />}
          >
            CREATE PRODUCT
          </Button>
        </div>
      </div>
    </div>
  );
};

ProductCreation.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React, { useState, FC, ReactElement } from 'react';
import './App.css';
import { Button, Checkbox, Input } from 'antd';
import { PlusOutlined } from '@ant-design/icons';
const Parse = require('parse/dist/parse.min.js');

export const ProductCreation: FC<{}> = (): ReactElement => {
  // State variables
  const [productName, setProductName] = useState('');
  const [productQuantity, setProductQuantity] = useState('');
  const [productPrice, setProductPrice] = useState('');
  const [productAvailable, setProductAvailable] = useState(false);
  const [productExpirationDate, setProductExpirationDate] = useState('');
  const [productCategories, setProductCategories] = useState('');

  const createProduct = async function (): Promise<boolean> {
    try {
      // These values come from state variables
      // Convert data values to corresponding data types
      const productNameValue: string = productName;
      const productQuantityValue: number = Number(productQuantity);
      const productPriceValue: number = Number(productPrice);
      const productAvailableValue: boolean = productAvailable;
      const productExpirationDateValue: Date = new Date(productExpirationDate);
      const productCategoriesValue: string[] = productCategories.split(',');

      // Creates a new Product parse object instance
      let Product: Parse.Object = new Parse.Object('Product');

      // Set data to parse object
      Product.set('name', productNameValue);
      Product.set('quantity', productQuantityValue);
      Product.set('price', productPriceValue);
      Product.set('available', productAvailableValue);
      Product.set('expirationDate', productExpirationDateValue);
      Product.set('categories', productCategoriesValue);
      Product.set('completeData', {
        name: productNameValue,
        quantity: productQuantityValue,
        price: productPriceValue,
        available: productAvailableValue,
        expirationDate: productExpirationDateValue,
        categories: productCategoriesValue,
      });

      // After setting the values, save it on the server
      try {
        let savedProduct: Parse.Object = await Product.save();
        // Success
        alert(`Success! ${JSON.stringify(savedProduct)}`);
        return true;
      } catch (error) {
        // Error can be caused by lack of Internet connection
        alert(`Error! ${error.message}`);
        return false;
      }
    } catch (error: any) {
      // Error can be caused by wrong type of values in fields
      alert(`Error! ${error.message}`);
      return false;
    }
  };

  return (
    <div>
      <div className="header">
        <img
          className="header_logo"
          alt="Back4App Logo"
          src={
            'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png'
          }
        />
        <p className="header_text_bold">{'React on Back4App'}</p>
        <p className="header_text">{'Product Creation'}</p>
      </div>
      <div className="container">
        {/* Product field inputs */}
        <div className="flex_between">
          <h2 className="list_heading">Available?</h2>
          <Checkbox
            onChange={(e) => setProductAvailable(e.target.checked)}
          ></Checkbox>
        </div>
        <div className="form_wrapper">
          <Input
            className="form_input"
            value={productName}
            onChange={(event) => setProductName(event.target.value)}
            placeholder="Name"
            size="large"
          />
          <Input
            className="form_input"
            value={productQuantity}
            onChange={(event) => setProductQuantity(event.target.value)}
            placeholder="Quantity"
            size="large"
          />
          <Input
            className="form_input"
            value={productPrice}
            onChange={(event) => setProductPrice(event.target.value)}
            placeholder="Price"
            size="large"
          />
          <Input
            className="form_input"
            value={productExpirationDate}
            onChange={(event) => setProductExpirationDate(event.target.value)}
            placeholder="Expiration Date (mm/dd/yyyy)"
            size="large"
          />
          <Input
            className="form_input"
            value={productCategories}
            onChange={(event) => setProductCategories(event.target.value)}
            placeholder="Categories (separated by comma)"
            size="large"
          />
          {/* Add product button */}
          <Button
            type="primary"
            className="form_button"
            color={'#208AEC'}
            size={'large'}
            onClick={createProduct}
            icon={<PlusOutlined />}
          >
            CREATE PRODUCT
          </Button>
        </div>
      </div>
    </div>
  );
};

App.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
html {
  box-sizing: border-box;
  outline: none;
  overflow: auto;
}

*,
*:before,
*:after {
  margin: 0;
  padding: 0;
  box-sizing: inherit;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  margin: 0;
}

p {
  margin: 0;
}

body {
  margin: 0;
  background-color: #fff;
}

.container {
  width: 100%;
  max-width: 600px;
  margin: auto;
  padding: 20px 0;
}

.header {
  align-items: center;
  padding: 25px 0;
  background-color: #208AEC;
}

.header_logo {
  height: 55px;
  margin-bottom: 20px;
  object-fit: contain;
}

.header_text_bold {
  margin-bottom: 3px;
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  font-weight: bold;
}

.header_text {
  color: rgba(255, 255, 255, 0.9);
  font-size: 15px;
}

.flex_between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.list_heading {
  font-weight: bold;
}

.form_wrapper {
  margin-top: 20px;
  margin-bottom: 10px;
}

.form_input {
  margin-bottom: 20px;
}

.form_button {
  width: 100%;
}

After setting up this screen, your application should look like this:

React Back4App

Note that each Product attribute has its text input field, except for the boolean checkbox input, meaning that the data in them needs conversion to the corresponding data type before saving.

Step 2 - Converting Input Data

Before saving your data to the Parse.object, you need to correctly format the number, DateTime, and array inputs. Let’s now create a saving function, which will retrieve data from your state variables and apply the suitable data conversion:

ProductCreation.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const createProduct = async function () {
  try {
    // These values come from state variables
    // Convert data values to corresponding data types
    const productNameValue = productName;
    const productQuantityValue = Number(productQuantity);
    const productPriceValue = Number(productPrice);
    const productAvailableValue = productAvailable;
    const productExpirationDateValue = new Date(productExpirationDate);
    const productCategoriesValue = productCategories.split(',');
  } catch (error) {
    // Error can be caused by wrong type of values in fields
    alert(`Error! ${error.message}`);
    return false;
  }
};

ProductCreation.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const createProduct = async function (): Promise<boolean> {
  try {
    // These values come from state variables
    // Convert data values to corresponding data types
    const productNameValue: string = productName;
    const productQuantityValue: number = Number(productQuantity);
    const productPriceValue: number = Number(productPrice);
    const productAvailableValue: boolean = productAvailable;
    const productExpirationDateValue: Date = new Date(productExpirationDate);
    const productCategoriesValue: string[] = productCategories.split(',');
  } catch (error: any) {
    // Error can be caused by wrong type of values in fields
    alert(`Error! ${error.message}`);
    return false;
  }
};

The number data conversion is done casting the value as a Number JavaScript object. DateTime is converted using the Date JavaScript object constructor; the array one is created by using the String.split method in JavaScript, creating an array containing each entry of the categories field separated by commas.

Note that your data is now contained inside a single object, which can be set in a new Parse.object instance to be saved to the server using the Parse.Object.set() method, which takes two arguments: the field name and the value to be set. Let’s also set a new field called completeData, which will be your object type field, assigning the same data object to it.

Go ahead and complete the createProduct function with the following:

ProductCreation.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const createProduct = async function () {
  try {
    // These values come from state variables
    // Convert data values to corresponding data types
    const productNameValue = productName;
    const productQuantityValue = Number(productQuantity);
    const productPriceValue = Number(productPrice);
    const productAvailableValue = productAvailable;
    const productExpirationDateValue = new Date(productExpirationDate);
    const productCategoriesValue = productCategories.split(',');

    // Creates a new Product parse object instance
    let Product = new Parse.Object('Product');

    // Set data to parse object
    Product.set('name', productNameValue);
    Product.set('quantity', productQuantityValue);
    Product.set('price', productPriceValue);
    Product.set('available', productAvailableValue);
    Product.set('expirationDate', productExpirationDateValue);
    Product.set('categories', productCategoriesValue);
    Product.set('completeData', {
      name: productNameValue,
      quantity: productQuantityValue,
      price: productPriceValue,
      available: productAvailableValue,
      expirationDate: productExpirationDateValue,
      categories: productCategoriesValue,
    });

    // After setting the values, save it on the server
    try {
      let savedProduct = await Product.save();
      // Success
      alert(`Success! ${JSON.stringify(savedProduct)}`);
      return true;
    } catch (error) {
      // Error can be caused by lack of Internet connection
      alert(`Error! ${error.message}`);
      return false;
    }
  } catch (error) {
    // Error can be caused by wrong type of values in fields
    alert(`Error! ${error.message}`);
    return false;
  }
};

ProductCreation.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const createProduct = async function (): Promise<boolean> {
  try {
    // These values come from state variables
    // Convert data values to corresponding data types
    const productNameValue: string = productName;
    const productQuantityValue: number = Number(productQuantity);
    const productPriceValue: number = Number(productPrice);
    const productAvailableValue: boolean = productAvailable;
    const productExpirationDateValue: Date = new Date(productExpirationDate);
    const productCategoriesValue: string[] = productCategories.split(',');

    // Creates a new Product parse object instance
    let Product: Parse.Object = new Parse.Object('Product');
    
    // Set data to parse object
    Product.set('name', productNameValue);
    Product.set('quantity', productQuantityValue);
    Product.set('price', productPriceValue);
    Product.set('available', productAvailableValue);
    Product.set('expirationDate', productExpirationDateValue);
    Product.set('categories', productCategoriesValue);
    Product.set('completeData', {
      name: productNameValue,
      quantity: productQuantityValue,
      price: productPriceValue,
      available: productAvailableValue,
      expirationDate: productExpirationDateValue,
      categories: productCategoriesValue,
    });

    // After setting the values, save it on the server
    try {
      let savedProduct: Parse.Object = await Product.save();
      // Success
      alert(`Success! ${JSON.stringify(savedProduct)}`);
      return true;
    } catch (error: any) {
      // Error can be caused by lack of Internet connection
      alert(`Error! ${error.message}`);
      return false;
    };
  } catch (error: any) {
    // Error can be caused by wrong type of values in fields
    alert(`Error! ${error.message}`);
    return false;
  }
};

You can now test the component, inserting the createProduct function in it, and calling it inside your form submit button onClick property. After creating a product, you should see an alert containing its data like this:

React Back4App

To certify that your data was saved on the server using the correct data types, you can look at your Parse dashboard. Click on the Product data table and note that every column has its data type written at the header. Your class should look like this:

React Back4App

Conclusion

At the end of this guide, you learned how to save each of the basic data types available on Parse using a React component. In the next guide, you will learn about the relational data on Parse.