[英]React Higher Order Components to add a custom attribute to the rendered JSX
I'm trying to write a React Higher Order Components to add a custom attribute "test_id" to the view of a wrappedComonent, I need that auto-genrated attribute to do some UI testing later. 我正在尝试编写一个React高阶组件来将自定义属性“test_id”添加到wrappedComonent的视图中,我需要这个自动生成的属性以便稍后进行一些UI测试。 but I have not find a way to achieve that. 但我还没有办法实现这一目标。
import React, {Component, PropTypes} from "react";
const wrapTestableComponent = (ComponentToWrap) => {
class TestableComponent extends Component {
constructor(props){
super(props);
}
render() {
return <ComponentToWrap {...this.props} test_id={this.props.test_id} />;
}
}
TestableComponent.propTypes = {
test_id: PropTypes.string.isRequired,
}
return TestableComponent
}
export default wrapTestableComponent;
I've also tried the below version but I got that error: Uncaught TypeError: Can't add property test_id, object is not extensible 我也试过以下版本,但是我得到了那个错误:未捕获TypeError:无法添加属性test_id,对象不可扩展
import React, {Component, PropTypes} from "react";
const wrapTestableComponent = (ComponentToWrap) => {
class TestableComponent extends Component {
constructor(props){
super(props);
}
render() {
var wrappedComponentView = <ComponentToWrap {...this.props} />;
wrappedComponentView.test_id = this.props.test_id;
return <ComponentToWrap {...this.props} />;
}
}
TestableComponent.propTypes = {
test_id: PropTypes.string.isRequired,
}
return TestableComponent
}
export default wrapTestableComponent;
EDIT 编辑
According to the comments we discussed below, I misunderstood the question before and I revised my answer. 根据我们下面讨论的评论,我之前误解了这个问题,并修改了我的答案。
The way you use in http://pastebin.com/0N9kKF73 should be the best way to do what you want. 您在http://pastebin.com/0N9kKF73中使用的方式应该是您想要的最佳方式。
I've tried to make a function that returns React.creatElement()
to make a copy and assigned the extra props for ComponentToWrap
but failed because of two main reasons. 我试图创建一个函数,返回React.creatElement()
来制作副本并为ComponentToWrap
分配额外的道具,但由于两个主要原因而失败。
type
. React.creatElement()需要一个param type
。 REF: REF:
The revised version from your pastebin and the internet. 来自您的pastebin和互联网的修订版本。
const wrapTestableComponent = (ComponentToWrap) => { class TestableComponent extends React.Component { componentDidMount() { ReactDOM.findDOMNode(this.wrappedRef).setAttribute('test_id', this.props.test_id); } render() { return <ComponentToWrap {...this.props} ref={() => { this.wrappedRef = this; }} />; } } TestableComponent.propTypes = { test_id: React.PropTypes.string.isRequired, } return TestableComponent } const TestComp = (props) => (<div>Here is the TestComp</div>) const NewComponent = wrapTestableComponent(TestComp) ReactDOM.render(<NewComponent test_id="555" />, document.getElementById('View'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="View"></div>
I suppose this is what are you trying to find. 我想这就是你想要找到的东西。 ref
may do all magic for you. ref
可以为你做所有的魔法。 Actually i dont have any others idea how to add custom component. 实际上我没有任何其他想法如何添加自定义组件。 fiddle
export function SelectWrapper(Select){
return class Wrapper extends Component {
componentDidMount(){
var element = ReactDOM.findDOMNode(this.refs.test);
element.setAttribute('custom-attribute', 'some value');
}
...
render(){
return <Select {...this.props} ref='test'/>
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.