创建react应用程序_如何使用React创建一个三层应用程序
創(chuàng)建react應(yīng)用程序
Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!
“發(fā)現(xiàn)功能JavaScript”被BookAuthority評(píng)為最佳新功能編程書籍之一 !
Splitting a Single Page Application into layers has a set of advantages:
將單頁(yè)應(yīng)用程序拆分為多個(gè)層具有一系列優(yōu)點(diǎn):
- a better separation of concerns 更好的關(guān)注點(diǎn)分離
- the layer implementation can be replaced 層實(shí)現(xiàn)可以替換
- the UI layer can be hard to test. By moving the logic to other layers, it becomes easier to test. UI層可能很難測(cè)試。 通過(guò)將邏輯移到其他層,測(cè)試變得更加容易。
Below we can see the diagram of an application split in the three main layers:
在下面,我們可以看到將應(yīng)用程序的圖分為三個(gè)主要層:
- UI (aka Presentation, View) 用戶界面(又名演示文稿,視圖)
- Domain (aka Business) 域(又名業(yè)務(wù))
- Data Access 資料存取
展示柜 (The showcase)
I’ll take the case of an application managing a list of to-dos. The user is able to see and search for to-dos.
我將以一個(gè)應(yīng)用程序管理待辦事項(xiàng)列表為例。 用戶能夠查看和搜索待辦事項(xiàng)。
檢查git-hub的完整實(shí)現(xiàn) 。 (Check the full implementation on git-hub.)
UI層 (UI Layer)
The UI layer is responsible for displaying data on the page, and for handling user interactions. The UI Layer is made up of components.
UI層負(fù)責(zé)在頁(yè)面上顯示數(shù)據(jù),并負(fù)責(zé)處理用戶交互。 UI層由組件組成。
I split the page in the following components:
我將頁(yè)面分為以下幾個(gè)部分:
TodoContainer manages the communication between TodoSearch, TodoList and other external objects
TodoContainer管理TodoSearch , TodoList與其他外部對(duì)象之間的通信
TodoSearchForm is the form for searching to-dos
TodoSearchForm是用于搜索待辦事項(xiàng)的表單
TodoList displays the list of to-dos
TodoList顯示待辦事項(xiàng)列表
TodoListItem: displays a single to-do in the list
TodoListItem:在列表中顯示一個(gè)待辦事項(xiàng)
待辦事項(xiàng)搜索 (TodoSearch)
The component uses the handleChange handler to read the input value on any change. TodoSearch exposes a new property: onSearch . It can be used by the parent component to handle the search click.
組件使用handleChange 處理程序以讀取任何更改的輸入值。 TodoSearch公開(kāi)了一個(gè)新屬性: onSearch 。 父組件可以使用它來(lái)處理搜索單擊。
The component doesn't communicate with any other external objects, except its parent. TodoSearch is a presentation component.
該組件不與其父對(duì)象以外的任何其他外部對(duì)象進(jìn)行通信。 TodoSearch是一個(gè)演示組件。
export default class TodoSearch extends React.Component { constructor(props){super(props);this.search = this.search.bind(this);this.handleChange = this.handleChange.bind(this);this.state = { text: "" };}search(){const query = Object.freeze({ text: this.state.text });if(this.props.onSearch)this.props.onSearch(query);}handleChange(event) {this.setState({text: event.target.value});}render() {return <form><input onChange={this.handleChange} value={this.state.text} /><button onClick={this.search} type="button">Search</button></form>;} }待辦事項(xiàng)清單 (TodoList)
TodoList gets the list of todos to render using a property. It sends the todos, one by one, to the TodoListItem.
TodoList獲取列表中todos使用屬性來(lái)呈現(xiàn)。 它發(fā)送todos ,一個(gè)接一個(gè),到TodoListItem 。
TodoList is a stateless functional component.
TodoList是無(wú)狀態(tài)功能組件。
export default function TodoList(props) {function renderTodoItem(todo){return <TodoListItem todo={todo} key={todo.id}></TodoListItem>;}return <div className="todo-list"><ul>{ props.todos.map(renderTodoItem) }</ul></div>; }TodoListItem (TodoListItem)
TodoListItem displays the todo received as a parameter. It is implemented as a stateless functional component.
TodoListItem將接收到的todo顯示為參數(shù)。 它被實(shí)現(xiàn)為無(wú)狀態(tài)功能組件。
export default function TodoListItem(props){return <li><div>{ props.todo.title}</div><div>{ props.todo.userName }</div></li>; }Read Functional Architecture with React and Redux and learn how to build apps in function style.
閱讀具有React和Redux的功能架構(gòu),并學(xué)習(xí)如何以函數(shù)樣式構(gòu)建應(yīng)用程序。
Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!
發(fā)現(xiàn)功能JavaScript被稱為 BookAuthority最好的新功能編程書籍 !
For more on applying functional programming techniques in React take a look at Functional React.
有關(guān)在React中應(yīng)用函數(shù)式編程技術(shù)的更多信息,請(qǐng)查看 Functional React 。
You can find me on Medium and Twitter.
您可以在Medium和Twitter上找到我。
翻譯自: https://www.freecodecamp.org/news/how-to-create-a-three-layer-application-with-react-8621741baca0/
創(chuàng)建react應(yīng)用程序
總結(jié)
以上是生活随笔為你收集整理的创建react应用程序_如何使用React创建一个三层应用程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到自己拉大便是什么征兆
- 下一篇: 虚拟dom添加虚拟dom_虚拟DOM缓慢