What is the best way to have variable attributes in JSX?

后端 未结 2 668
你的背包
你的背包 2021-02-03 10:22

Hopefully my question is clear, I\'m mainly looking for a way to dynamically attach attributes to a JSX input.



        
2条回答
  •  梦如初夏
    2021-02-03 10:53

    You can't do it the way you are doing. You need to define your attributes as object and pass that as spread attributes.

    The properties of the object that you pass in are copied onto the component's props.

    You can use this multiple times or combine it with other attributes.

    var Hello = React.createClass({
          render: function() {
            
            var opt = {}
            opt['placeholder'] = "enter text here";
            return (
    Hello {this.props.name}
    ); } }); ReactDOM.render( , document.getElementById('container') );
    
    
    
    

    DOCS

提交回复
热议问题