admin管理员组

文章数量:1290235

React.JS provides a variety of mechanisms for implementing ponents - class-based, ponents via React.JS hooks, and one can also use Styled-Components. In a web application some times one wants to set the readOnly property on an input HTML element. To do so via HTML one uses the readOnly property/attribute on the input HTML element. The confusing thing is that HTML property/attribute has no value:

<!-- Create a readOnly input field to display the username -->
<input id='username' name='username' value='fred.fish' readOnly />

In React.JS if one is using JSX when one defines a ponent, one can specify properties (props). The value of props are then specified using standard XML attribute/property syntax.

Here is an example of a React.JS Component:

const PageTitleHeading1 = (props) => {
    const {
        id,
        textLine1,
        textLine2,
        textLine3,
        showCompanyNameLogo
    } = props;

    if (id === undefined || id === null || id === '') {
        throw new Error('Bad call to BasePageTitleHeading1. id is required ' +
                        'and must not be undefined, null, or the empty string.');
    }

    const pageHeadingLine1 = (textLine1 !== undefined && textLine1 !== null &&  textLine1 !== '') ?
                             <PageHeadingLine1Style key='1' className='pageHeadingLine1Style'>{textLine1}</PageHeadingLine1Style> :
                             null;

    const pageHeadingLine2 = ((textLine2 !== undefined && textLine2 !== null &&  textLine2 !== '') || showCompanyNameLogo) ?
                             <PageHeadingLine2 key='2' className='PageHeadingLine2' showCompanNameLogo={showCompanyNameLogo ? true : false} text={(textLine2 !== undefined && textLine2 !== null) ? textLine2 : ''}/> :
                             null;

    const pageHeadingLine3 = (textLine3 !== undefined && textLine3 !== null &&  textLine3 !== '') ?
                             <PageHeadingLine3Style key='3' className='PageHeadingLine3Style'>{textLine3}</PageHeadingLine3Style> :
                             null;

    return (
        <PageHeading1 id={id} className={`pageHeading1 ${props.className}`}>
            {pageHeadingLine1}
            {pageHeadingLine2}
            {pageHeadingLine3}
        </PageHeading1>
    );
}

I can the use the ponent using standard JSX syntax specifying its properties using standard XML attributes/properties:

        <PageTitleHeading1
            id='change-your-password-page-title-heading'
            textLine1='Wele to the'
            textLine2=' On-Demand Training Platform'
            textLine3='Please change your password'
            showCompanyNameLogo={true}
        />

So what if I want to render a React.JS ponent that is an input field and set the readOnly attribute/property? This is a little confusing because in HTML the readOnly attribute/property in an input field has no value <input id='username' name='username' value='fred.fish' readOnly /> where in React.JS when one specifies properties/attributes to be used in a ponent, the properties/attributes have values. So how does one set the readOnly property on a React.JS ponent that renders an input HTML element/entity?

Here is an example of a ponent that renders an input field using Styled-Components. How does one render a React.JS Component that has the ReadOnly property set on it?

const StyledInputField = styled.input`
    width: ${props.width ? props.width : '100%'};
    height: ${props.height ? props.height : '42px'};
    padding: 10px 10px 10px 10px';
    font-family: Montserrat, sans-serif;
    font-size: 16;
    font-weight: 600;
    text-align: left;
`

React.JS provides a variety of mechanisms for implementing ponents - class-based, ponents via React.JS hooks, and one can also use Styled-Components. In a web application some times one wants to set the readOnly property on an input HTML element. To do so via HTML one uses the readOnly property/attribute on the input HTML element. The confusing thing is that HTML property/attribute has no value:

<!-- Create a readOnly input field to display the username -->
<input id='username' name='username' value='fred.fish' readOnly />

In React.JS if one is using JSX when one defines a ponent, one can specify properties (props). The value of props are then specified using standard XML attribute/property syntax.

Here is an example of a React.JS Component:

const PageTitleHeading1 = (props) => {
    const {
        id,
        textLine1,
        textLine2,
        textLine3,
        showCompanyNameLogo
    } = props;

    if (id === undefined || id === null || id === '') {
        throw new Error('Bad call to BasePageTitleHeading1. id is required ' +
                        'and must not be undefined, null, or the empty string.');
    }

    const pageHeadingLine1 = (textLine1 !== undefined && textLine1 !== null &&  textLine1 !== '') ?
                             <PageHeadingLine1Style key='1' className='pageHeadingLine1Style'>{textLine1}</PageHeadingLine1Style> :
                             null;

    const pageHeadingLine2 = ((textLine2 !== undefined && textLine2 !== null &&  textLine2 !== '') || showCompanyNameLogo) ?
                             <PageHeadingLine2 key='2' className='PageHeadingLine2' showCompanNameLogo={showCompanyNameLogo ? true : false} text={(textLine2 !== undefined && textLine2 !== null) ? textLine2 : ''}/> :
                             null;

    const pageHeadingLine3 = (textLine3 !== undefined && textLine3 !== null &&  textLine3 !== '') ?
                             <PageHeadingLine3Style key='3' className='PageHeadingLine3Style'>{textLine3}</PageHeadingLine3Style> :
                             null;

    return (
        <PageHeading1 id={id} className={`pageHeading1 ${props.className}`}>
            {pageHeadingLine1}
            {pageHeadingLine2}
            {pageHeadingLine3}
        </PageHeading1>
    );
}

I can the use the ponent using standard JSX syntax specifying its properties using standard XML attributes/properties:

        <PageTitleHeading1
            id='change-your-password-page-title-heading'
            textLine1='Wele to the'
            textLine2=' On-Demand Training Platform'
            textLine3='Please change your password'
            showCompanyNameLogo={true}
        />

So what if I want to render a React.JS ponent that is an input field and set the readOnly attribute/property? This is a little confusing because in HTML the readOnly attribute/property in an input field has no value <input id='username' name='username' value='fred.fish' readOnly /> where in React.JS when one specifies properties/attributes to be used in a ponent, the properties/attributes have values. So how does one set the readOnly property on a React.JS ponent that renders an input HTML element/entity?

Here is an example of a ponent that renders an input field using Styled-Components. How does one render a React.JS Component that has the ReadOnly property set on it?

const StyledInputField = styled.input`
    width: ${props.width ? props.width : '100%'};
    height: ${props.height ? props.height : '42px'};
    padding: 10px 10px 10px 10px';
    font-family: Montserrat, sans-serif;
    font-size: 16;
    font-weight: 600;
    text-align: left;
`
Share Improve this question asked Apr 19, 2020 at 14:49 Peter Jirak EldritchPeter Jirak Eldritch 8213 gold badges9 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

In React.JS, if you are using JSX, then readOnly functions in a ponent as a boolean property.

Below is a styled ponent (See Styled-Components)

const StyledInputField = styled.input`
    width: ${props.width ? props.width : '100%'};
    height: ${props.height ? props.height : '42px'};
    padding: 10px 10px 10px 10px';
    font-family: Montserrat, sans-serif;
    font-size: 16;
    font-weight: 600;
    text-align: left;
`

You do not really have to know or understand Styled Components to understand this answer. Now suppose I want to set the readOnly attribute on a ponent that I create from my StyledInputField ponent declaration?

<StyledInputField type="text" readOnly={true} />

React.JS interprets ReadOnly as the ReadOnly attribute and interprets its having a truthy value as the given ponent should be rendered with the readOnly attribute set on it.

This will in effect give you the following:

CSS:

.styledInputField {
    width: ${props.width ? props.width : '100%'};
    height: ${props.height ? props.height : '42px'};
    padding: 10px 10px 10px 10px';
    font-family: Montserrat, sans-serif;
    font-size: 16;
    font-weight: 600;
    text-align: left;
}

HTML:

<input type="text" class="styledInputField" readOnly />

React.JS recognizes the readOnly property and handles it appropriately. One may also use a state value or a prop

Example - here I am using props.isReadOnly to set the readOnly attribute on StyledInputField:

<StyledInputField type="text" readOnly={props.isReadOnly} />

If props.isReadOnly is truthy, then the readOnly property will be set. If props.isReadOnly is falsy, it will not be set.

  • True and False vs. "Truthy" and "Falsey" (or "Falsy") in Ruby, Python, and JavaScript
  • How to Set the readOnly TextBox in React.JS TextBox ponent

I originally posted this answer here How to use as readonly . I am sharing it here on StackExchange as I am sure other React.JS developers need to render readOnly input fields and may be struggling a little to figure out the syntax. I hope this helps.

本文标签: