1

I am using below in one of my Child component. I would like

{...this.props.class ? "className= 'form-control "+ this.props.class +"'": "className= 'form-control'"}

I am using this in Parent component like this.

class=""

I am getting Warning: Invalid attribute name: 23 error in console.

2 Answers 2

1

className={this.props.className? "form-control " + this.props.className: "form-control"}

You can't use class as props variable.
It is included in the javascript predefined words which should not be used as variable name. https://www.w3schools.com/js/js_reserved.asp

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks @think-serious. Should I use className ?
Yes, you can use className.
...this.props.class, also I don't know what is this used for? @abuabu
Changed my answer. You would need something like that.
Thanks @think-serious. Your solution is working. Thanks.
|
1

You can have a prop named className in the child component and then pass className ="my-class" to your child.

In the child component, a cleaner way of conditionally adding classes is :

className = {`form-control ${this.props.className || ""}`}

This says: if props.className is passed, add that, otherwise use empty string and form-control will always be there regardless of what is passed down from parent which seems to be your case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.