基本使用
1 安装依赖
- 步骤:
- 使用
npm install安装必要的依赖,如ngrok、react-dom和react-scripts。 - 如果使用
react-scripts,可能还需要安装react-scripts-browser。
- 使用
- 示例代码:
npm install --save-dev react-dom npm install --save-dev react-scripts npm install --save-dev react-scripts-browser
2 创建项目
- 步骤:
- 打开
node_modules.js,创建一个新的项目,添加app.js。 - 打开
ngrok,访问http://localhost:88。
- 打开
- 示例代码:
npm run dev
3 创建 React 应用
-
步骤:
- 创建一个新文件
index.js,使用React和useState。 - 使用
use React和use useState。
- 创建一个新文件
-
示例代码:
import React from 'react'; import { useState } from 'react'; const App = () => { const [name, setName] = useState(''); return ( <div className="container"> <h1>Hello World</h1> <p>{name}</p> </div> ); };
配置
1 项目配置文件
- 步骤:
- 在项目目录下创建
.config.json。 - 按照配置文件格式填写项目信息,如名称、版本、本地构建工具和开发者模式。
- 在项目目录下创建
- 示例配置:
{ name: " Brook App", version: "1..", build: "ngrok", dev: "ngrok", scripts: { build: "ngrok", start: "ngrok", }, cachePath: "./app", debug: false, theme: "light" }
2 项目配置文件夹
- 步骤:
- 在项目目录下创建
app目录。 - 在
app目录下创建build目录,包含ngrok的配置。
- 在项目目录下创建
- 示例配置:
npm run dev
高级功能
1 使用 ngrok
-
步骤:
- 使用
ngrok来展示 API。 - 在项目中使用
ngrok的配置,如启动 web 端。
- 使用
-
示例代码:
import { createWebPort } from 'ngrok'; const { webPort } = createWebPort({ host: 'http://localhost:8', port: 88, kid: 123, }); app.on('click', () => { webPort.start(); });
2 使用 React 和 ngrok
-
步骤:
- 使用
ngrok的 React 工具,如ngrok-react. - 将
ngrok-react插入index.js。
- 使用
-
示例代码:
import React from 'react'; import { use, useState } from 'react'; import 'ngrok-react'; const useNgrok = use(ngrokreact.Ngrok).use('ngrok-react'); const app = use(React).useNS('app', useState((name) => 'root')) { const { run } = useNgrok().run(); return ( <div className="container"> <h1>{name}</h1> <p>Web端:{run('http://localhost:8')}</p> </div> ); };
使用 React 模块
1 使用 React 模块
-
步骤:
- 使用
use React和use TypeModule。 - 定义 React 模块。
- 使用
-
示例代码:
import React from 'react'; import { use } from 'react'; import TypeModule from 'react-type'; import Type from 'react-type'; import TypeList from 'react-type'; import TypeUnion from 'react-type'; import TypeSet from 'react-type'; import TypeExpr from 'react-type'; import TypeLiteral from 'react-type'; import TypeUnionType from 'react-type'; import TypeUnionTypeSet from 'react-type'; function MyModule({ name, version }) { return ( <div className="container"> <h1>{name}</h1> <p>{version}</p> </div> ); }
常用技巧
1 优化数据加载
-
示例代码:
const items = []; function加载Data() { items.push({ id: Date.now(), name: 'Test', }); localStorage.setItem('testData', JSON.stringify(items)); } const serveData = async () => { await fetch('http://localhost:8/api/v1'); const response = await fetch(...); const data = await response.json(); localStorage.setItem('testData', JSON.stringify(data)); }; loading = false; serveData = async () => { if (!loading) { loading = true; try { serveData(); } finally { loading = false; } } }; serveData();
2 状态管理
-
示例代码:
const state = { name: 'Test' }; const handleNameChange(newName) { state.name = newName; }; state.on('name', (name) => handleNameChange(name));
3 编译配置
- 示例代码:
import React from 'react'; import { useState } from 'react'; import { App } from './app'; const App = () => { const [name, setName] = useState(''); return ( <div className="container"> <h1>{name}</h1> <p>{name}</p> </div> ); };
案例
1 基本应用
- 示例代码:
const React = require('react'); const App = () => { return ( <div className="container"> <h1>Hello World</h1> <p>Welcome to Brook App</p> </div> ); };
2 使用高级功能
-
示例代码:
const App = () => { return ( <div className="container"> <h1>Welcome to Brook App</h1> <div className="row"> <div className="col-md-4"> <h2>Welcome to Brook</h2> <div className="text-center"> <p>Welcome to Brook's frontend</p> <button onClick={() => App.startWebPort()} className="btn btn-primary">Start</button> </div> </div> <div className="col-md-4"> <h2>Advanced Features</h2> <div className="text-center"> <p>Use React components with Brook's web UI</p> <button onClick={() => App.startWebPort({ kid: '123', kidEmail: 'john@example.com', kidName: 'John Doe' })} className="btn btn-primary">Advanced</button> </div> </div> </div> </div> ); }; const { startWebPort } = React.use((app, startWebPort) => { startWebPort({ host: 'http://localhost:8', kid: '123', }); }); return App;npm run start
发生问题时的建议
- 查阅官方文档:如
ngrok、react-scripts的官方文档。 - 在线资源:如 Stack Overflow、GitHub Issues。
- 社区讨论:如 Stack Overflow、Reddit 的 #brout-discuss 话题。
通过以上教程,你将获得 Brook客户端的基础知识和高级使用技巧,能够高效地开发前端应用,实践是关键,多做项目,不断优化代码,才能真正



