You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/learn/add-react-to-an-existing-project.md
+35-35
Original file line number
Diff line number
Diff line change
@@ -1,59 +1,59 @@
1
1
---
2
-
title: Add React to an Existing Project
2
+
title: 將 React 加入到一個現有的專案
3
3
---
4
4
5
5
<Intro>
6
6
7
-
If you want to add some interactivity to your existing project, you don't have to rewrite it in React. Add React to your existing stack, and render interactive React components anywhere.
**You need to install [Node.js](https://nodejs.org/en/) for local development.**Although you can [try React](/learn/installation#try-react) online or with a simple HTML page, realistically most JavaScript tooling you'll want to use for development requires Node.js.
13
+
**你需要在開發環境安裝 [Node.js](https://nodejs.org/en/)。**雖然你可以在線上或是使用簡單的 HTML 頁面[嘗試 React](/learn/installation#try-react),但實際上,大多數你想要用於開發的 JavaScript 工具都需要 Node.js。
14
14
15
15
</Note>
16
16
17
-
## Using React for an entire subroute of your existing website {/*using-react-for-an-entire-subroute-of-your-existing-website*/}
Let's say you have an existing web app at `example.com` built with another server technology (like Rails), and you want to implement all routes starting with `example.com/some-app/`fully with React.
1.**Build the React part of your app** using one of the [React-based frameworks](/learn/start-a-new-react-project).
24
-
2.**Specify `/some-app`as the *base path*** in your framework's configuration (here's how: [Next.js](https://nextjs.org/docs/api-reference/next.config.js/basepath), [Gatsby](https://www.gatsbyjs.com/docs/how-to/previews-deploys-hosting/path-prefix/)).
25
-
3.**Configure your server or a proxy** so that all requests under `/some-app/`are handled by your React app.
This ensures the React part of your app can [benefit from the best practices](/learn/start-a-new-react-project#can-i-use-react-without-a-framework) baked into those frameworks.
Many React-based frameworks are full-stack and let your React app take advantage of the server. However, you can use the same approach even if you can't or don't want to run JavaScript on the server. In that case, serve the HTML/CSS/JS export ([`next export`output](https://nextjs.org/docs/advanced-features/static-html-export) for Next.js, default for Gatsby) at`/some-app/`instead.
Let's say you have an existing page built with another technology (either a server one like Rails, or a client one like Backbone), and you want to render interactive React components somewhere on that page. That's a common way to integrate React--in fact, it's how most React usage looked at Meta for many years!
1.**Set up a JavaScript environment** that lets you use the [JSX syntax](/learn/writing-markup-with-jsx), split your code into modules with the [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) / [`export`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)syntax, and use packages (for example, React) from the [npm](https://www.npmjs.com/) package registry.
38
-
2.**Render your React components** where you want to see them on the page.
A modular JavaScript environment lets you write your React components in individual files, as opposed to writing all of your code in a single file. It also lets you use all the wonderful packages published by other developers on the [npm](https://www.npmjs.com/) registry--including React itself! How you do this depends on your existing setup:
***If your app is already split into files that use `import`statements,** try to use the setup you already have. Check whether writing `<div />`in your JS code causes a syntax error. If it causes a syntax error, you might need to [transform your JavaScript code with Babel](https://babeljs.io/setup), and enable the [Babel React preset](https://babeljs.io/docs/babel-preset-react)to use JSX.
***If your app doesn't have an existing setup for compiling JavaScript modules,** set it up with [Vite](https://vitejs.dev/). The Vite community maintains [many integrations with backend frameworks](https://github.com/vitejs/awesome-vite#integrations-with-backends), including Rails, Django, and Laravel. If your backend framework is not listed, [follow this guide](https://vitejs.dev/guide/backend-integration.html) to manually integrate Vite builds with your backend.
If the entire content of your page was replaced by a "Hello, world!", everything worked! Keep reading.
83
+
如果你的整個網頁內容都被替換成了「Hello, world!」,那麼一切正常!請繼續閱讀。
84
84
85
85
<Note>
86
86
87
-
Integrating a modular JavaScript environment into an existing project for the first time can feel intimidating, but it's worth it! If you get stuck, try our [community resources](/community) or the [Vite Chat](https://chat.vitejs.dev/).
Of course, you don't actually want to clear the existing HTML content!
106
+
當然,你實際上不想清除現有的 HTML 內容!
107
107
108
-
Delete this code.
108
+
刪除這段程式碼。
109
109
110
-
Instead, you probably want to render your React components in specific places in your HTML. Open your HTML page (or the server templates that generate it) and add a unique [`id`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) attribute to any tag, for example:
110
+
相反地,你可能想要在 HTML 中的特定位置 render 你的 React component。打開你的 HTML 頁面(或產生它的 server template),並為任何 tag 加入一個唯一的 [`id`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) attribute,例如:
111
111
112
112
```html
113
113
<!-- ... somewhere in your html ... -->
114
114
<navid="navigation"></nav>
115
115
<!-- ... more html ... -->
116
116
```
117
117
118
-
This lets you find that HTML element with [`document.getElementById`](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById)and pass it to [`createRoot`](/reference/react-dom/client/createRoot) so that you can render your own React component inside:
118
+
這讓你可以使用 [`document.getElementById`](https://developer.mozilla.org/zh-TW/docs/Web/API/Document/getElementById)找到該 HTML 元素,並將其傳遞給 [`createRoot`](/reference/react-dom/client/createRoot),以便你可以在其中 render 自己的 React component:
Notice how the original HTML content from `index.html`is preserved, but your own `NavigationBar` React component now appears inside the `<nav id="navigation">`from your HTML. Read the [`createRoot`usage documentation](/reference/react-dom/client/createRoot#rendering-a-page-partially-built-with-react) to learn more about rendering React components inside an existing HTML page.
149
+
注意原始的 HTML 內容從 `index.html`被保留下來,但是你自己的 `NavigationBar` React component 現在出現在你 HTML 中的 `<nav id="navigation">`內。閱讀 [`createRoot`使用文件](/reference/react-dom/client/createRoot#rendering-a-page-partially-built-with-react)以了解更多關於在現有 HTML 頁面中 render React component 的資訊。
150
150
151
-
When you adopt React in an existing project, it's common to start with small interactive components (like buttons), and then gradually keep "moving upwards" until eventually your entire page is built with React. If you ever reach that point, we recommend migrating to [a React framework](/learn/start-a-new-react-project)right after to get the most out of React.
[React Native](https://reactnative.dev/)can also be integrated into existing native apps incrementally. If you have an existing native app for Android (Java or Kotlin) or iOS (Objective-C or Swift), [follow this guide](https://reactnative.dev/docs/integration-with-existing-apps) to add a React Native screen to it.
0 commit comments