Recat2
Learn More →
In this lesson, we're going to move away from our application being plain HTML and convert it to being powered by React. To do that, we'll need to add a number of libraries:
Here are the packages that we'll be adding in the next video:
Learn More →
Here's the commit with the changes made in this video.
The changes we've just implemented should look pretty familiar - they were just converting parts of our app from HTML to being powered by React Components.
Alrighty, so you've learned React. You've built Redux and used it in a regular HTML application. But now we've started converting that HTML to a React application. In the following video we're going to start connecting the React Components to the Redux store.
I want you to pay attention to a few things in the next screencast:
store.dispatch()
code goes in a React componentHere's the commit with the changes made in this video.
In order to save time, we used an uncontrolled component for our input field.
ref
Refs provide a way to access DOM nodes or React elements created in the render method.
The docs outline a few good use cases for refs:
- Managing focus, text selection, or media playback.
- Triggering imperative animations.
- Integrating with third-party DOM libraries.
Let's take a look at a similar example:
In the line ref={(inputElement) => this.colorElement = inputElement}
, inputElement
is a reference to the input DOM element. We are storing a reference to the input DOM element in the colorElement instance property of the Color class.
Please note:
React will call the ref callback with the DOM element when the component mounts, and call it with
null
when it unmounts. Refs are guaranteed to be up-to-date beforecomponentDidMount
orcomponentDidUpdate
fires.
總結: ref我覺得他是在react組裝好之後才會進行呼叫,但是又比componentDidMount以及componentDidUpdate這些生命週期執行之前執行
Learn More →
Here's the commit with the changes made in this video.
Learn More →
Here's the commit with the changes made in this video.
Learn More →
Here's the commit with the changes made in this video.
Learn More →
Here's the commit with the changes made in this video.
In this section, we converted our plain HTML application to one using React Components. We didn't implement any new features. Instead, we just improved the code's organization by breaking out separate parts into reusable chunks.
Learn More →
Read these articles: Component State vs Redux Store and React + Redux Architecture : Separation of Concerns. Answer the following questions and share your answers with your classmates:
Explain how React interplays with Redux.
Give an example that illustrates the Separation of Concerns Principle.
參考: http://skyroxas.tw/react-js-jsx-與-babel-簡介/
我們會利用JSX來產生html的element,但因為很多主流的瀏覽器不支援JSX因此我們需要透過Babel來針對JSX進行編譯,Babel不僅可以編譯JSX還可以編譯ES6
參考: https://ithelp.ithome.com.tw/articles/10198999
原本預期如果點其裡面的input那就會連動啟動外面div的onClick
https://ithelp.ithome.com.tw/upload/images/20180829/20106935SEmaP5JSxg.jpg
使用event.stopPropagation
來阻止事件冒泡
https://ithelp.ithome.com.tw/upload/images/20180829/201069356c7lJXaO0i.jpg
使用event.preventDefault
來阻止預設的動作
<a>
這個DOM本身帶有跳轉畫面的功能,如果同時在DOM裡面寫function以及url,點擊的時候會呼叫function之後就立即跳轉業面了,但是添加event.preventDefault可以讓a失去跳轉業面的功能只有執行onclick參考: https://www.fooish.com/reactjs/component-lifecycle.html#render
constructor()
就可以做代替他的工作有類似的功能render 他其實是react中生命週期update的一環
componentWillReceiveProps(nextProps)
if (this.props.initialX !== nextProps.initialX)
shouldComponentUpdate(nextProps, nextState)
componentWillUpdate
render
componentDidUpdate
都依定會執行一次,vise versa, if return false, then will not call these three functions…componentWillUpdate(nextProps, nextState)
getSnapshotBeforeUpdate(prevProps, prevState)
componentDidUpdate(prevProps, prevState, snapshot)
元件方法 (Method) 有 setState()
,而屬性 (Properties) 有 props
和 state
。
forceUpdate()
後 React 會跳過 shouldComponentUpdate()
(可能怕return false後面都不用跑 那forceUpdate就沒有意義) 直接執行 render()
(但子元件的 Lifecycle Methods 都會被正常執行)。元件生命週期事件順序
整理一下整個 Component 的生命週期發生的事件順序。
當元件第一次 render 時的順序:
此後,當元件被更新 (update) 時的順序:
其中不能執行 this.setState() 的事件: 簡單來說就是render之前的生命週期
參考: https://medium.com/netscape/component-state-vs-redux-store-1eb0c929277
首先我們可以把react的component分成smart and dump
那何時會使用redux?