I want to get the text content of the react element. In the example below, I want get: hello 1
var node = <myComponent id={1} />
class myComponent extends React.Component {
render() {
return {
<span> hello {this.props.id} </span>
}
}
I tried to use node.text
or ReactDOM.findDOMNode(node).textContent
, but it doesn't work. Can I get some help?
Thanks!
嗨,我认为你应该像下面这样修改你的代码:
<myComponent id={1} />
Try calling your code using:
ReactDOM.render(<MyComponent id={1} />, document.getElementById("root"));
Your code:
class MyComponent extends React.Component {
render() {
return <span>Hello {this.props.id}</span>;
}
}
Link to this code in a Codesandbox: https://codesandbox.io/s/class-component-dv6fj
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.