### Basic Navigation Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/router/ZMPRouter
Example demonstrating the use of useNavigate hook for navigating between pages.
```APIDOC
## Example Usage
This example shows how to use the `useNavigate` hook to navigate from the Home Page to Page 1.
```javascript
import React from "react";
import { Page, Text, Box, Button, useNavigate } from "zmp-ui";
export default function HomePage(props) {
const navigate = useNavigate();
return (
Home Page
);
}
```
```
--------------------------------
### Snackbar Usage Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.7.0/Snackbar
Demonstrates how to import and use the useSnackbar hook to open and manage snackbars. Includes setup for closing snackbars on component unmount.
```javascript
import React, { useRef, useEffect } from "react";
import { useSnackbar, Button, Page, Box, Text } from "zmp-ui";
export default function HomePage() {
const { openSnackbar, setDownloadProgress, closeSnackbar } = useSnackbar();
const timmerId = useRef();
useEffect(
() => () => {
closeSnackbar();
clearInterval(timmerId.current);
},
[]
);
return (
Snackbar
```
--------------------------------
### Install Zalo Mini App CLI
Source: https://miniapp.zaloplatforms.com/documents/devtools/cli/intro
Install the Zalo Mini App CLI globally using npm. Ensure Node.js is installed beforehand.
```bash
npm install -g zmp-cli
```
--------------------------------
### Basic List Usage Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.10.0/data-display/List
An example demonstrating how to use the List and ListItem components to display a simple list with various configurations.
```APIDOC
## Example Usage
### HomePage.js
```javascript
import React from "react";
import { Page, List, Icon, Button } from "zmp-ui";
const { Item } = List;
export default function HomePage(props) {
return (
} />
}
suffix={}
/>
}
subTitle="subtitle"
/>
} />
}
/>
);
}
```
```
--------------------------------
### Navigation Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.10.0/router/ZMPRouter
Example demonstrating the usage of the useNavigate hook to navigate between pages in a Zalo Mini App.
```APIDOC
## Example Usage
```javascript
import React from "react";
import { Page, Text, Box, Button, useNavigate } from "zmp-ui";
export default function HomePage(props) {
const navigate = useNavigate();
return (
Home Page
);
}
```
```
--------------------------------
### Install zmp-sdk
Source: https://miniapp.zaloplatforms.com/documents/api
Install the Zalo Mini App SDK using npm. This is the first step to interact with the Zalo Mini App API.
```bash
npm install zmp-sdk
```
--------------------------------
### Page Component Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.7.0/container/Page
An example demonstrating how to use the Page component with its properties.
```APIDOC
## Example Usage
### HomePage.js
```javascript
import React from "react";
import { Page, Input } from "zmp-ui";
export default function HomePage(props) {
return (
);
}
```
```
--------------------------------
### Bottom Navigation Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/navigation/bottom-navigation
Example of how to implement Bottom Navigation in a React application using zmp-ui.
```APIDOC
## Example
HomePage.js
```javascript
import React, { useState } from "react";
import { BottomNavigation, Icon, Page } from "zmp-ui";
const BottomNavigationPage = (props) => {
const [activeTab, setActiveTab] = useState("chat");
const { title } = props;
return (
setActiveTab(key)}
>
}
activeIcon={}
/>
}
activeIcon={}
/>
}
activeIcon={}
/>
}
activeIcon={}
/>
}
activeIcon={}
/>
);
};
export default BottomNavigationPage;
```
```
--------------------------------
### Install zaui-tokens Package
Source: https://miniapp.zaloplatforms.com/documents/zaui/foundation/colors
Install the zaui-tokens package via npm to access color tokens and other design variables.
```bash
npm install zaui-tokens
```
--------------------------------
### Page Component Usage Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.9.0/container/Page
An example demonstrating how to use the Page component with its properties.
```APIDOC
## Example
### HomePage.js
```javascript
import React from "react";
import { Page, Input } from "zmp-ui";
export default function HomePage(props) {
return (
);
}
```
```
--------------------------------
### Basic Tabs Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/layout/tabs
A simple example demonstrating the basic usage of the Tabs component with 'Mini Apps', 'Community', and 'Support' as tab items.
```javascript
function App() {
return (
Khám phá
Mở Zalo, bấm quét QR để quét
và khám phá Mini Apps trên điện thoại
Cộng đồng nhà phát triển Mini App
Tham gia nhóm Facebook
);
}
```
--------------------------------
### Install zmp-ui Package
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.10.0
Use npm to install the zmp-ui package for Zalo Mini App development.
```bash
npm install zmp-ui
```
--------------------------------
### Default Swiper Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.7.0/data-display/Swiper
A basic example demonstrating how to implement a default Swiper component with multiple slides.
```APIDOC
## Example: Swiper Mặc định
```javascript
import React from "";
import { Page, Swiper, Box, Text } from "zmp-ui";
export default function HomePage() {
return (
Swiper
);
}
```
```
--------------------------------
### Swiper with Autoplay Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.7.0/data-display/Swiper
An example demonstrating how to enable autoplay for the Swiper component, with a specified duration.
```APIDOC
## Example: Swiper autoplay
```javascript
import React from "";
import { Page, Swiper, Box, Text } from "zmp-ui";
export default function HomePage() {
return (
Swiper
);
}
```
```
--------------------------------
### Tabs Component Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.8.0/tabs
An example demonstrating how to use the Tabs component in a Zalo Mini App, including setting up multiple tabs with content.
```APIDOC
## Example
```javascript
import React from "react";
import { Page, Tabs, List, Icon, Avatar } from "zmp-ui";
const alphabet = "abcdefghijklmnopqrstuvwxyz";
const users = Array.from(Array(10).keys()).map((i) => ({
name: `Người dùng ${i}`,
avatar: alphabet[Math.floor(Math.random() * alphabet.length)].toUpperCase(),
online: Math.floor(Math.random() * 10) % 2 === 0,
key: i,
}));
export default function HomePage(props) {
return (
{users.map((user) => (
{user.avatar}}
title={user.name}
subTitle="subtitle"
suffix={}
/>
))}
Tab 2 content
Tab 3 content
);
}
```
```
--------------------------------
### Example Server Upload Response (Success)
Source: https://miniapp.zaloplatforms.com/documents/api/openMediaPicker
This is an example of a successful response from a media upload server API.
```json
{
"error": 0,
"message": "Success",
"data": {
"urls": [...]
}
}
```
--------------------------------
### ImageViewer Usage Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.8.0/data-display/ImageViewer
An example demonstrating how to use the ImageViewer component in a React Zalo Mini App. It shows how to manage the visibility and active index of the viewer and how to trigger its opening by clicking on thumbnail images.
```APIDOC
## Example Usage
```javascript
import React, { useState } from "react";
import { Page, ImageViewer, Box, Text } from "zmp-ui";
const images = [
{
src: "https://stc-zmp.zadn.vn/zmp-zaui/images/e2e10aa1a6087a5623192.jpg",
alt: "img 1",
key: "1"
},
{
src: "https://stc-zmp.zadn.vn/zmp-zaui/images/fee40cbea0177c4925061.jpg",
alt: "img 2",
key: "2"
},
{
src: "https://stc-zmp.zadn.vn/zmp-zaui/images/82ca759bd932056c5c233.jpg",
alt: "img 3",
key: "3"
},
{
src: "https://stc-zmp.zadn.vn/zmp-zaui/images/77f5b8cd1464c83a91754.jpg",
alt: "img 4",
key: "4"
}
];
export default function HomePage() {
const [visible, setVisible] = useState(false);
const [activeIndex, setActiveIndex] = useState(0);
return (
ImageViewerUsage
Khi cần xem và tương tác với hình ảnh trên kích thước lớn hơn
Example
{images.map((img, index) => (
{
setActiveIndex(index);
setVisible(true);
}}
src={img.src}
alt={img.alt}
/>
))}
setVisible(false)}
activeIndex={activeIndex}
images={images}
visible={visible}
/>
);
}
```
```
--------------------------------
### Basic Slider Examples
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.10.0/input-field/Slider
Demonstrates various configurations of the Slider component, including default values, max values, labels, prefixes, suffixes, and range selection. Use these examples to understand how to customize the Slider for different use cases.
```javascript
import React from "react";
import { Box, Text, Slider, Page, Icon } from "zmp-ui";
function PageSlider() {
return (
Slider}
suffix={}
/>
00:00}
suffix={04:30}
/>
);
}
export default PageSlider;
```
--------------------------------
### Slider Examples
Source: https://miniapp.zaloplatforms.com/documents/zaui/input-field/Slider
Illustrates various ways to use the Slider component with different properties and configurations.
```APIDOC
## Slider Examples
### Description
Examples demonstrating the usage of the Slider component in different scenarios.
### Basic Slider
```javascript
import React from "react";
import { Box, Slider } from "zmp-ui";
```
### Slider with Max Value and Show Value
```javascript
import React from "react";
import { Box, Slider } from "zmp-ui";
```
### Slider with Prefix and Suffix Icons
```javascript
import React from "react";
import { Box, Slider, Icon } from "zmp-ui";
}
suffix={}
/>
```
### Slider with Custom Prefix and Suffix Text
```javascript
import React from "react";
import { Box, Slider, Text } from "zmp-ui";
00:00}
suffix={04:30}
/>
```
### Range Slider
```javascript
import React from "react";
import { Box, Slider } from "zmp-ui";
```
### Slider with Marks
```javascript
import React from "react";
import { Box, Slider } from "zmp-ui";
```
### Range Slider with Custom Marks
```javascript
import React from "react";
import { Box, Slider } from "zmp-ui";
```
```
--------------------------------
### Face Retrieval Result Response Example
Source: https://miniapp.zaloplatforms.com/documents/ekyc/apis/faceRetrievalResult
This is an example of the JSON response body returned by the face retrieval API. The 'data' field contains encrypted information that needs to be decrypted to get validation details for ID numbers and faces.
```json
{
"code": 0,
"message": "Successful",
"data": "",
"sign": "8f4642cdb4c555f9a6d5926f4df9b73e44da2e675512d65d44c0a212",
"request_id": "12345678"
}
```
--------------------------------
### Open Media Picker with Async/Await
Source: https://miniapp.zaloplatforms.com/documents/api/openMediaPicker
This example demonstrates opening the media picker using async/await for a more modern asynchronous flow. It includes error handling for API failures.
```javascript
import { openMediaPicker } from "zmp-sdk/apis";
const pickMedia = async () => {
try {
const { data } = await openMediaPicker({
type: "photo",
serverUploadUrl: "https:///upload/media",
compressLevel: 2, //Tùy chọn nếu cần nén ảnh
});
const result = JSON.parse(data);
console.log(result);
} catch (error) {
// xử lý khi gọi api thất bại
console.log(error);
}
};
```
--------------------------------
### Get Native Storage Info
Source: https://miniapp.zaloplatforms.com/documents/api/getNativeStorageInfo
This function retrieves the current and limit sizes of the native storage cache synchronously. It is supported starting from SDK version 2.43.0.
```APIDOC
## getStorageInfo
### Description
Retrieves information about the native storage cache synchronously.
### Method
`nativeStorage.getStorageInfo()`
### Parameters
This method does not take any parameters.
### Response
Returns a `Promise` containing the cache information.
#### StorageInfo
- **currentSize** (`number`) - The current space occupied by the cache in KB.
- **limitSize** (`number`) - The limit size of the cache space in KB.
### Request Example
```javascript
const { currentSize, limitSize } = nativeStorage.getStorageInfo();
```
### Response Example
```json
{
"currentSize": 1024,
"limitSize": 5120
}
```
### Version Support
- SDK: 2.43.0 and above
```
--------------------------------
### Get Current Network Type
Source: https://miniapp.zaloplatforms.com/documents/api/getNetworkType
Call the getNetworkType function to retrieve the current network type. The result is a Promise that resolves to an object containing the networkType. Handle potential errors by referring to the error handling guide.
```javascript
const { networkType } = await getNetworkType();
```
--------------------------------
### Basic Grid Layout Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.10.0/layout/Grid
Demonstrates how to use the Grid component to create a layout with a specified number of columns and spacing. Ensure Zalo UI components are imported.
```javascript
import React from "react";
import { Page, Grid, Button, Center } from "zmp-ui";
export default function HomePage() {
return (
);
}
```
--------------------------------
### Get Current App Name and Version
Source: https://miniapp.zaloplatforms.com/documents/api/getAppInfo
Call the getAppInfo API to retrieve the current Zalo Mini App's name and live version. Ensure you handle potential errors by referring to the error handling guide.
```javascript
const { name, version } = await getAppInfo({});
```
--------------------------------
### Switch Component Examples
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.10.0/input-field/Switch
Demonstrates various configurations of the Switch component, including different sizes, checked states, and disabled states. Import Switch from 'zmp-ui'.
```javascript
import React from "react";
import { Page, Switch, Text } from "zmp-ui";
export default function HomePage(props) {
return (
Switch
Medium Size
Small Size
);
}
```
--------------------------------
### Khởi tạo dự án Zalo Mini App
Source: https://miniapp.zaloplatforms.com/documents/devtools/cli/commands/init
Sử dụng lệnh `zmp init` để tạo một dự án Zalo Mini App mới. Lệnh này sẽ tạo cấu trúc thư mục và cài đặt các dependencies cần thiết.
```bash
zmp init
```
--------------------------------
### Page Component Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/container/Page
An example demonstrating how to use the Page component with the `hideScrollbar` property.
```APIDOC
## Page Component Example
### HomePage.js
```javascript
import React from "react";
import { Page, Input } from "zmp-ui";
export default function HomePage(props) {
return (
);
}
```
```
--------------------------------
### Swiper Autoplay Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/data-display/Swiper
This example shows how to enable and configure the autoplay feature for the Swiper component.
```APIDOC
## Swiper Autoplay
### Description
This example demonstrates how to enable and configure the autoplay feature for the Swiper component, including setting the duration for transitions.
### Properties Used
- **autoplay** (boolean) - Enables automatic slide transitions.
- **duration** (number) - Sets the time in milliseconds for each slide before transitioning.
- **loop** (boolean) - Enables continuous looping of slides.
### Example
```javascript
import React from "react";
import { Page, Swiper, Box, Text } from "zmp-ui";
export default function HomePage() {
return (
Swiper
);
}
```
```
--------------------------------
### Basic async/await usage example
Source: https://miniapp.zaloplatforms.com/documents/api/errorCode
Demonstrates the basic structure of using async/await for API calls. Remember that `await` must be used inside an `async` function.
```javascript
const { userInfo } = await getUserInfo({
autoRequestPermission: true,
});
const { orders } = await fetchOrders({
userId: userInfo.id,
});
console.log("Các đơn hàng của người dùng: ", orders);
```
--------------------------------
### Avatar Usage Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.8.0/data-display/Avatar
Example demonstrating the usage of the Avatar component with different sizes and variations.
```APIDOC
## Example
```javascript
import React from "react";
import { Avatar, Text, Box, Page } from "zmp-ui";
const { Group } = Avatar;
function AvatarPage() {
return (
<>
Size16px24px32x40px48px64px80pxVariationGroup 1
>
);
}
```
--------------------------------
### Deploy Zalo Mini App using CLI
Source: https://miniapp.zaloplatforms.com/documents/intro/convert-web-app-to-zalo-mini-app
Use the Zalo Mini App CLI to deploy your existing project. Follow the prompts to select the 'Deploy your existing project' option and specify the build folder.
```bash
zmp deploy
```
--------------------------------
### Basic Bottom Sheet and Action Sheet Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.9.0/sheet
Demonstrates how to use both a custom bottom sheet for displaying information and an action sheet for menu options. Ensure the Sheet and Sheet.Actions components are imported from 'zmp-ui'.
```javascript
import React, { useRef, useState } from "react";
import { Button, Sheet, Text, Box, Page } from "zmp-ui";
export default function Demo() {
const [sheetVisible, setSheetVisible] = useState(false);
const [actionSheetVisible, setActionSheetVisible] = useState(false);
return (
Bottom Sheet setSheetVisible(false)}
autoHeight
mask
handler
swipeToClose
>
Cho phép Starbucks Coffee xác định vị trí của bạn
Starbucks Coffee sẽ sử dụng vị trí của bạn để hỗ trợ giao nhận
hàng, tìm kiếm dịch vụ, bạn bè quanh bạn, hoặc các dịch vụ liên
quan đến địa điểm khác.
setActionSheetVisible(false)}
swipeToClose
actions={[
[
{ text: "Sample Menu", close: true },
{ text: "Sample Menu", close: true },
{
text: "Negative Menu",
danger: true,
close: true,
},
],
[{ text: "Cancel", close: true }],
]}
/>
);
}
```
--------------------------------
### Import getSetting API
Source: https://miniapp.zaloplatforms.com/documents/api/getSetting
Import the getSetting API from the zmp-sdk/apis library. This is a prerequisite for using the API.
```javascript
import { getSetting } from "zmp-sdk/apis";
```
--------------------------------
### Example Server Upload Response (Failure)
Source: https://miniapp.zaloplatforms.com/documents/api/openMediaPicker
This is an example of a failed response from a media upload server API.
```json
{
"error": -1,
"message": "Fail"
}
```
--------------------------------
### Navigate to Page 1 using useNavigate
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.10.0/router/ZMPRouter
This example demonstrates how to use the `useNavigate` hook to navigate to '/page1' when a button is clicked. Ensure `ZMPRouter` is set up to handle the routing.
```javascript
import React from "react";
import { Page, Text, Box, Button, useNavigate } from "zmp-ui";
export default function HomePage(props) {
const navigate = useNavigate();
return (
Home Page
);
}
```
--------------------------------
### Basic Tabs Implementation
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.10.0/tabs
Demonstrates how to implement a basic Tabs component with multiple tab panes, each containing different content. This example shows how to structure tabs with labels and associated content, including a list of users in the first tab.
```javascript
import React from "react";
import { Page, Tabs, List, Icon, Avatar } from "zmp-ui";
const alphabet = "abcdefghijklmnopqrstuvwxyz";
const users = Array.from(Array(10).keys()).map((i) => ({
name: `Người dùng ${i}`,
avatar: alphabet[Math.floor(Math.random() * alphabet.length)].toUpperCase(),
online: Math.floor(Math.random() * 10) % 2 === 0,
key: i,
}));
export default function HomePage(props) {
return (
{users.map((user) => (
{user.avatar}}
title={user.name}
subTitle="subtitle"
suffix={}
/>
))}
Tab 2 content
Tab 3 content
);
}
```
--------------------------------
### Create Payment Order with Minimal Details (Async/Await)
Source: https://miniapp.zaloplatforms.com/documents/payment/createOrder
This snippet demonstrates creating a payment order using async/await syntax with essential details. It includes a description, item list, amount, MAC, and a fail callback. The orderId is destructured from the resolved promise.
```javascript
const { orderId } = await Payment.createOrder({
desc: "Thanh toán 50.000",
item: [
{ id: "1", amount: 20000 },
{ id: "2", amount: 30000 },
],
amount: 50000,
mac: "YOUR MAC",
fail: (err) => {
// Tạo đơn hàng lỗi
console.log(err);
},
});
```
--------------------------------
### Page Component Usage Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.10.0/container/Page
An example demonstrating how to use the Page component in a React application for Zalo Mini Apps, including setting the `hideScrollbar` property.
```APIDOC
## Example
```javascript
import React from "react";
import { Page, Input } from "zmp-ui";
export default function HomePage(props) {
return (
);
}
```
```
--------------------------------
### Cấu hình Maven cho SDK Open API
Source: https://miniapp.zaloplatforms.com/documents/open-apis/open/set-up-client
Thêm dependency này vào file pom.xml của bạn để sử dụng SDK Open API. Thay thế `{your_path_to_file_sdk}` bằng đường dẫn thực tế đến file JAR của SDK.
```xml
com.vng.zalozalo-miniapp-openapi-sdk2.4.0system{your_path_to_file_sdk}
```
--------------------------------
### Slider Component Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.7.0/input-field/Slider
This example demonstrates how to use the Slider component with various configurations, including setting default values, labels, prefixes, suffixes, and range selections.
```APIDOC
## Slider Example
### HomePage.js
```javascript
import React from "react";
import { Box, Text, Slider, Page, Icon } from "zmp-ui";
function PageSlider() {
return (
Slider}
suffix={}
/>
00:00}
suffix={04:30}
/>
);
}
export default PageSlider;
```
```
--------------------------------
### ImageViewer Usage Example
Source: https://miniapp.zaloplatforms.com/documents/zaui/1.9.0/data-display/ImageViewer
This example demonstrates how to use the ImageViewer component in a React application. It shows how to manage the visibility and active index of the viewer, and how to trigger its display when images are clicked.
```APIDOC
## ImageViewer Usage Example
### Example
```javascript
import React, { useState } from "react";
import { Page, ImageViewer, Box, Text } from "zmp-ui";
const images = [
{
src: "https://stc-zmp.zadn.vn/zmp-zaui/images/e2e10aa1a6087a5623192.jpg",
alt: "img 1",
key: "1",
},
{
src: "https://stc-zmp.zadn.vn/zmp-zaui/images/fee40cbea0177c4925061.jpg",
alt: "img 2",
key: "2",
},
{
src: "https://stc-zmp.zadn.vn/zmp-zaui/images/82ca759bd932056c5c233.jpg",
alt: "img 3",
key: "3",
},
{
src: "https://stc-zmp.zadn.vn/zmp-zaui/images/77f5b8cd1464c83a91754.jpg",
alt: "img 4",
key: "4",
},
];
export default function HomePage() {
const [visible, setVisible] = useState(false);
const [activeIndex, setActiveIndex] = useState(0);
return (
ImageViewerUsage
When you need to view and interact with images at a larger size.
Example
{images.map((img, index) => (
{
setActiveIndex(index);
setVisible(true);
}}
src={img.src}
alt={img.alt}
/>
))}
setVisible(false)}
activeIndex={activeIndex}
images={images}
visible={visible}
/>
);
}
```
```