步骤1:创建项目目录并初始化npm
打开终端,进入一个新的目录,执行以下命令:
mkdir mykitsunebi cd mykitsunebi npm init -y
步骤2:安装Kitsunebi
在项目目录中运行以下命令,安装Kitsunebi和相关依赖:
npm install kitsunebi
步骤3:创建组件
创建一个src目录,添加一个新的组件文件:
mkdir -p src touch src/MyComponent.js
编辑src/MyComponent.js
// src/MyComponent.js
import React from 'react';
import { Kitsunebi } from 'kitsunebi';
const MyComponent = () => {
return (
<div>
<h1>Hello, Kitsunebi!</h1>
</div>
);
};
export default MyComponent;
步骤4:创建主应用文件
创建App.js文件:
touch App.js
编辑App.js
// App.js
import React from 'react';
import { Kitsunebi } from 'kitsunebi';
import MyComponent from './MyComponent';
const App = () => {
return (
<div>
<h1>Kitsunebi应用</h1>
<Kitsunebi>
<MyComponent />
</Kitsunebi>
</div>
);
};
export default App;
步骤5:运行应用
在项目根目录中运行以下命令,启动Kitsunebi应用:
npm run kitsunebi start
应用将自动启动,打开浏览器访问http://localhost:300查看结果。
步骤6:配置路由
创建一个路由配置文件src/MyPage.js:
touch src/MyPage.js
编辑MyPage.js:
// src/MyPage.js
import React from 'react';
import { Kitsunebi } from 'kitsunebi';
const MyPage = () => {
return (
<div>
<h1>我的页面</h1>
</div>
);
};
export default MyPage;
在App.js中添加路由配置:
// App.js
import React from 'react';
import { Kitsunebi } from 'kitsunebi';
import MyComponent from './MyComponent';
import MyPage from './MyPage';
const App = () => {
return (
<div>
<h1>Kitsunebi应用</h1>
<Kitsunebi>
<MyComponent />
<MyPage path="/page" />
</Kitsunebi>
</div>
);
};
export default App;
步骤7:使用全局状态
在另一个组件中使用全局状态:
创建src/GlobalState.js:
touch src/GlobalState.js
编辑GlobalState.js:
// src/GlobalState.js
import { useStore } from 'kitsunebi';
const GlobalState = () => {
const store = useStore();
return (
<div>
<h2>全局状态</h2>
<p>当前状态: {store.state}</p>
<button
onClick={() => {
store.setState({ message: 'Hello, Kitsunebi!' });
}}
>
更新状态
</button>
</div>
);
};
export default GlobalState;
在App.js中导入并显示全局状态:
// App.js
import React from 'react';
import { Kitsunebi } from 'kitsunebi';
import MyComponent from './MyComponent';
import MyPage from './MyPage';
import GlobalState from './GlobalState';
const App = () => {
return (
<div>
<h1>Kitsunebi应用</h1>
<Kitsunebi>
<MyComponent />
<MyPage path="/page" />
<GlobalState />
</Kitsunebi>
</div>
);
};
export default App;
步骤8:处理数据获取(示例)
创建src/DataFetching.js:
touch src/DataFetching.js
编辑DataFetching.js:
// src/DataFetching.js
import { useStore } from 'kitsunebi';
import { fetch } from 'kitsunebi';
const DataFetching = () => {
const store = useStore();
const getData = async () => {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
store.setState({ fetchedData: data });
} catch (error) {
console.error('数据获取失败:', error);
}
};
return (
<div>
<h2>数据获取</h2>
<button onClick={getData}>获取数据</button>
{store.fetchedData && (
<div>
<p>获取的数据: {store.fetchedData}</p>
</div>
)}
</div>
);
};
export default DataFetching;
步骤9:处理组件的生命周期(可选)
在App.js中添加组件的生命周期方法:
// App.js
import React from 'react';
import { Kitsunebi } from 'kitsunebi';
import MyComponent from './MyComponent';
import MyPage from './MyPage';
import GlobalState from './GlobalState';
import DataFetching from './DataFetching';
const App = () => {
React.useEffect(() => {
console.log('App组件被渲染');
}, []);
React.useEffect(() => {
console.log('App组件被更新');
}, []);
return (
<div>
<h1>Kitsunebi应用</h1>
<Kitsunebi>
<MyComponent />
<MyPage path="/page" />
<GlobalState />
<DataFetching />
</Kitsunebi>
</div>
);
};
export default App;
步骤10:配置Kitsunebi路由(可选)
在Kitsunebi中可以配置路由,修改kitsunebi.js文件:
// kitsunebi.js
const Kitsunebi = ({ children, path }) => {
return (
<div>
<h2>路由配置</h2>
<div>{children}</div>
</div>
);
};
Kitsunebi路由 = {
"/": Kitsunebi,
"/page": Kitsunebi,
};
步骤11:使用Kitsunebi组件作为路由容器
在App.js中将所有组件包裹在Kitsunebi中,并传递路径:
// App.js
import React from 'react';
import { Kitsunebi } from 'kitsunebi';
import MyComponent from './MyComponent';
import MyPage from './MyPage';
import GlobalState from './GlobalState';
import DataFetching from './DataFetching';
const App = () => {
return (
<div>
<h1>Kitsunebi应用</h1>
<Kitsunebi>
<MyComponent />
<MyPage path="/page" />
<GlobalState />
<DataFetching />
</Kitsunebi>
</div>
);
};
export default App;
步骤12:处理错误和状态管理
在GlobalState.js中添加错误处理:
// src/GlobalState.js
import { useStore } from 'kitsunebi';
const GlobalState = () => {
const store = useStore();
React.useEffect(() => {
console.log('全局状态被更新');
}, [store.state]);
return (
<div>
<h2>全局状态</h2>
<p>当前状态: {store.state}</p>
<button
onClick={() => {
store.setState({ message: 'Hello, Kitsunebi!' });
}}
>
更新状态
</button>
</div>
);
};
export default GlobalState;
步骤13:部署应用(可选)
当应用开发完成后,可以使用kitsunebi build命令构建并部署到服务器。
步骤14:使用Kitsunebi CLI(可选)
使用Kitsunebi CLI可以自动化构建和部署,查看文档获取更多信息。
通过以上步骤,你已经成功安装并创建了一个简单的Kitsunebi应用,包括组件、路由、状态管理和数据获取,继续探索Kitsunebi的功能和特性,创建更复杂的应用。









