react-addons-text-content
Like DOM API's Node.textContent
and it works in React Element
Install
$ npm install --save react-addons-text-content
Usage
import textContent from 'react-addons-text-content'
render() {
const {children} = this.props
const text = textContent(children)
return (...)
}
Examples
test('should be got text `Hello World`', assert => {
const Com =
<div>
<h1>Hello</h1> World
</div>
assert.is(textContent(Com), `Hello World`)
})
test('array children', assert => {
const Com = (
<div>
<h1>Hello</h1> World
{
['a', <h2>b</h2>]
}
</div>
)
assert.is(textContent(Com), `Hello Worldab`)
})
test('array component', assert => {
const Com = (
[
<h1>a</h1>, 'b', 'c',
<div>
<h2>x</h2>y
</div>
]
)
assert.is(textContent(Com), `abcxy`)
})