GithubHelp home page GithubHelp logo

Comments (4)

b12031106 avatar b12031106 commented on April 19, 2024 2

(1)
理論上是,不過如果你沒有用到 state 的話,
那是不是就應該直接用 Functional Component 的寫法,
而不是 ES6 Class component 呢?

(2)
關鍵在 handleClick 執行的時候,this 代表什麼,
在教學的前一篇 Props、State、Refs 與表單處理 中的 js 範例裡有提到:

// 與 ES5 React.createClass({}) 不同的是 component 內自定義的方法需要自行綁定 this context,或是使用 arrow function 
this.tick = this.tick.bind(this);

官方的說明也有解釋為什麼要這樣做,可以參考看看:
https://facebook.github.io/react/docs/reusable-components.html#es6-classes

from reactjs101.

kdchang avatar kdchang commented on April 19, 2024 1

@b12031106 感謝協助~:)

@kuolun 感謝您的提問

(1)
若是需要綁定 this.方法或是需要在 constructor 使用 props,定義 state,就需要 constructor。若是在其他方法(如 render)使用 this.props 則不用一定要定義 constructor。
不過如果只有需要話 render UI 的話,Functional Component 是比較好的寫法。

關於 React ES6 class constructor super() 解釋可以參考 React ES6 class constructor super()

(2)
關於 Javascript this 用法可以參考 Javascript:this用法整理

希望對你有幫助,謝謝:)

from reactjs101.

kuolun avatar kuolun commented on April 19, 2024

感謝兩位的回覆,我研究研究那些參考連結內容先
謝謝
👍

from reactjs101.

kdchang avatar kdchang commented on April 19, 2024

@kuolun 不會~那我先 close issue 囉,有問題再 reopen ,謝謝~

from reactjs101.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.