reactjs - 将 React Typescript 应用程序转换为 Nextjs
问题描述
我是 Nextjs 的新手,我已经使用 Reactjs + Typescript 构建了一个 Web 应用程序,我想将其转换为 Nextjs。我对去哪里和安装部分的文件结构感到困惑。当前的 Web 应用程序(带有 Typescript 的 Reactjs)有一个数据库(Postdb)和如下功能:
- 创建新笔记
- 评论笔记
- 喜欢和不喜欢这张纸条。
有没有办法可以将这个现有项目顺利转换为 nextjs?
这也是我的文件结构
(New-App)
* node_modules
* public
* index.html
* manifest.json
* robots.txt
* src
* App.tsx
* db.tsx
* serviceWorker.ts
* index.tsx
* components
*Notes.tsx
*Header.tsx
*List.tsx
*pages
*index.tsx
*Quest.tsx
*Page.tsx
* test (has all the test files)
* images
*build
解决方案
Nextjs 的 pages 目录与 route 相关联(参见此链接),最好将 SSR 逻辑从每个 react 组件中分离出来。
所以我更喜欢页面文件有路由和 SSR 逻辑,组件文件有模板和客户端逻辑。
例如。
页面/index.tsx
import Notes from '../components/notes'
function HomePage({ notes }) {
return (
<div>
<Notes notes={notes}></Notes>
</div>
)
}
HomePage.getInitialProps = async ({ req }) => {
// DO SSR
const res = await fetch('https://some/api/endpoint')
const json = await res.json()
return { notes: json.notes }
}
export default HomePage
组件/注释/index.tsx
import Note from './Note';
export default (props) => {
return (
<div>
{
props.notes && props.notes.map((note) => <Note note={note}></Note>)
}
</div>
)
}
组件/注释/Note.tsx
export default (props) => {
return (
<div>
<p>comment {props.note.comment}</p>
<p>like {props.note.like}</p>
<p>dislike {props.note.dislike}</p>
</div>
)
}
推荐阅读
- firebase - 更新firebase插件更新后颤振中的“then”问题
- python-3.x - 在 Python 中将 txt 文件转换为字典
- python - 在 tweepy Streaming 中拉推文时在末尾排除链接
- typescript - 无法在不同浏览器的打字稿中使用 Element.requestFullscreen()
- typescript - 打字稿自定义类型不可分配给自定义类型
- flutter - 如何滚动到 ListView 中的下一项
- javascript - TypeError:无法访问属性“错误”,数据未定义
- django - 将参数传递给 Django 中导航栏菜单的模板
- c# - XSLT 1.0 - 用 HTML 包装的多个子节点模板
- python-2.7 - python2.7 paramiko - 远程运行脚本