React ignores 'for' attribute of the label element

前端 未结 6 1864
太阳男子
太阳男子 2020-12-02 06:30

In React (Facebook\'s framework), I need to render a label element bound to a text input using the standard for attribute.

e.g. the following JSX is use

相关标签:
6条回答
  • 2020-12-02 06:33

    Yes, for react,

    for becomes htmlFor

    class becomes className

    etc.

    see full list of how HTML attributes are changed here:

    https://facebook.github.io/react/docs/dom-elements.html

    0 讨论(0)
  • 2020-12-02 06:35

    For React you must use it's per-define keywords to define html attributes.

    class -> className

    is used and

    for -> htmlFor

    is used, as react is case sensitive make sure you must follow small and capital as required.

    0 讨论(0)
  • 2020-12-02 06:35

    That is htmlFor in JSX and class is className in JSX

    0 讨论(0)
  • 2020-12-02 06:40

    both for and class are reserved words in JavaScript this is why when it comes to HTML attributes in JSX you need to use something else, React team decided to use htmlFor and className respectively

    0 讨论(0)
  • 2020-12-02 06:41

    just using react htmlFor to replace for!

    you can find more info by following the below links.

    https://facebook.github.io/react/docs/dom-elements.html#htmlfor

    https://github.com/facebook/react/issues/8483

    https://github.com/facebook/react/issues/1819

    0 讨论(0)
  • 2020-12-02 06:55

    The for attribute is called htmlFor for consistency with the DOM property API. If you're using the development build of React, you should have seen a warning in your console about this.

    0 讨论(0)
提交回复
热议问题