Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 向JSX添加自定义属性_Reactjs_Typescript_Jsx_Typescript Typings - Fatal编程技术网

Reactjs 向JSX添加自定义属性

Reactjs 向JSX添加自定义属性,reactjs,typescript,jsx,typescript-typings,Reactjs,Typescript,Jsx,Typescript Typings,向JSX添加自定义属性的最佳方法是什么? Facebook说它已经通过React v16实现了 是的。但是如果我理解正确,我必须像mycustomattribute=“foo”那样做。这对我不起作用。因为我需要像MyCustomAttribute=“foo” 有什么想法吗?您指的是组件吗?在javascript中,您不能使用该对流,因为它会抛出一个错误“uncaughtsyntaxerror:Unexpected token-”,我建议您使用最接近您的情况的snake_case。但我真正建议的是

向JSX添加自定义属性的最佳方法是什么? Facebook说它已经通过React v16实现了

是的。但是如果我理解正确,我必须像
mycustomattribute=“foo”
那样做。这对我不起作用。因为我需要像
MyCustomAttribute=“foo”


有什么想法吗?

您指的是组件吗?在javascript中,您不能使用该对流,因为它会抛出一个错误“uncaughtsyntaxerror:Unexpected token-”,我建议您使用最接近您的情况的snake_case。但我真正建议的是始终使用camelCase。

您可以在下面向组件添加自定义属性

预期的父组件:

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: 'React'
    };
  }

  render() {
     let selDynamicAttributes = {"data-attr":"someString", "MyCustomAttribute":"foo"}; // see here added custom attrib
    return (
      <div>
        <Button {...selDynamicAttributes}  /> //passed custom attributes
      </div>
    );
  }
}
类应用程序扩展组件{
构造函数(){
超级();
此.state={
名称:“反应”
};
}
render(){
让selDynamicAttributes={“data attr”:“someString”,“MyCustomAttribute”:“foo”};//请参见此处添加的自定义属性
返回(
//传递的自定义属性
);
}
}
预期子组件:

 export default class Button extends React.Component {
    constructor(props) {
        super(props);
    }
    render(){
        const { ...otherAttributes } = this.props;
        return(
            <div>
                    <button 
                        {...otherAttributes}>
                            {this.props.displayText}
                    </button>
            </div>
        );      
    }
}
导出默认类按钮扩展React.Component{
建造师(道具){
超级(道具);
}
render(){
const{…otherAttributes}=this.props;
返回(
{this.props.displayText}
);      
}
}


更多信息请访问:

否。我需要一个通用的Windows应用程序。因此,我必须在a中添加一个名为“IsTabStop”的属性。这将有助于提供一个您已经尝试过的示例