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

后端 未结 2 667
你的背包
你的背包 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 11:08

    You can initialize an object with a computed property name, and then use JSX Spread Attributes to convert it to attribute:

    const DemoComponent = ({ variablePropName, variablePropValue }) => { 
        const variableAttribute = { [variablePropName]: variablePropValue };
        return (
            
        );
    };
    

提交回复
热议问题