Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kitce/react-with-traverse

Traverse into React component tree and transform the deepest strings to anything
https://github.com/kitce/react-with-traverse

Last synced: about 1 month ago
JSON representation

Traverse into React component tree and transform the deepest strings to anything

Awesome Lists containing this project

README

        

# react-with-traverse
A HOC (Higher-Order Component) that allows you to traverse into the React component tree to do something on each of the deepest strings, such as highlight keywords, censor sensitive content and etc.
## Installation
```bash
# npm
npm install react-with-traverse

# yarn
yarn add react-with-traverse
```
## Usage
This module exposes a single function.
```javascript
// ES6+
import withTraverse from 'react-with-traverse';

// ES5
const withTraverse = require('react-with-traverse');
```
### withTraverse(transform : Function) : Component
#### transform(child : Any, props : Object) : Node

`child` : Each of the deepest strings in the component tree (probably ``, unless you do something like `

{}
`)

`props` : The props for the result component

Use this function to transform `child` into anything you want by returning it.

Example:
```javascript
const convertToSmileEmoji = (child, props) => {
const {large} = props;
if (child === ':smile:') {
const src = large ? 'smile_large.png' : 'smile.png';
return Smiley face;
}
return child;
};
const Smile = withTraverse(convertToSmileEmoji);



Some text
:smile:


```
Result :
```html

Some text
Smiley face

```
## Test
(Test cases are also examples)
```bash
# npm
npm install
npm test

# yarn
yarn install
yarn test