最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - unable add required attribute to a ReactJS input component - Stack Overflow

programmeradmin6浏览0评论

I am attempting to add a required attribute to a react input ponent:

export default function UiInput() {
   render() {
   return (
    <input
     {...customAttributes}
     size={size ? size : null}
     value={inputValue}
     maxLength={maxLength}
     required={required}
    />
   )

and when I am calling my ponent like so,

<UiInput 
   required={required}
  />

I am not getting the red asterisk to render - not getting any errors when I pass in required to an input ponent, but no red asterisk is showing up, how can I make sure the asterisk renders for required inputs? Does ReactJS not support this?

I am attempting to add a required attribute to a react input ponent:

export default function UiInput() {
   render() {
   return (
    <input
     {...customAttributes}
     size={size ? size : null}
     value={inputValue}
     maxLength={maxLength}
     required={required}
    />
   )

and when I am calling my ponent like so,

<UiInput 
   required={required}
  />

I am not getting the red asterisk to render - not getting any errors when I pass in required to an input ponent, but no red asterisk is showing up, how can I make sure the asterisk renders for required inputs? Does ReactJS not support this?

Share Improve this question asked Jun 5, 2017 at 0:08 sfmaysfsfmaysf 1033 silver badges5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Remember that you are passing required down as a prop to UiInput.

The pattern you're using for your Component is a Stateless Functional Component, when you do this:

  • props gets passed through as a parameter.
  • You don't need to declare the render() method, just simply write your return statement.

You can declare your Component like so:

function UiInput(props) {
      return (
        <input
         size={props.size ? props.size : null}
         required={props.required}
        />
      )
    }

And render it like so:

<UiInput required="required" />

You can see a JSFiddle here. Please be aware that I did remove some props for the purpose of this demo.

Here's the Component rendered, please ignore the data-reactroot attribute.

发布评论

评论列表(0)

  1. 暂无评论