JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 42
  • Score
    100M100P100Q68908F
  • License ISC

React hashtags parser with custom renderer and click action on each hashtag.

Package Exports

  • react-hashtag

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-hashtag) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

React Hashtag

Enhance your strings with live components.

npm version codecov.io Code Coverage Build Status

Features:

  • Super small 264 B
  • Custom renderer for each hashtag
  • Custom 'click' handler for each hashtag
  • Generic output
  • Drop-in and use it. Your code will not have to adapt to anything.

Quick example

// Your typical 'component'
const Card = () => (
    <p>
        Here goes my card contents with #static text inside
    </p>
);

// Will become
const Card = () => (
    <p>
        <ReactHashtag>
            Here goes my card contents with #static text inside
        </ReactHashtag>
    </p>
);

Install

The usual flow

npm install react-hashtag --save

Api

The component ReactHashtag is actually pretty generic. Is not something that someone can't do in half an hour. But, this one has some generic API that could make you turn.

renderHashtag(hashtagValue, onClickHandler): Function

Returns the custom element to be renderer instead of a <span>. You can go wild here.

onHashtagClick(hashtagValue, event): Function

The click handler for each hashtag.

Example

Custom renderer

const Card = (props) => (
    <p>
        <ReactHashtag
            renderHashtag={(hashtagValue) => (
                <div className="hashtag">{hashtagValue}</div>
            )}
        >
            {props.children}
        </ReactHashtag>
    </p>
);

With styled components

const Hashtag = styled.span`
    color: tomato;
`;

const Card = (props) => (
    <p>
        <ReactHashtag
            renderHashtag={(hashtagValue) => (
                <Hashtag>{hashtagValue}</Hashtag>
            )}
        >
            {props.children}
        </ReactHashtag>
    </p>
);

Reusable or composition

You could reuse the same definition, if that's something you're looking for. The following example uses the withRouter from React Router and defines a component that will redirect to certain hashtag pages.

const StyledHashtag = styled.span`
    color: tomato;
`;

/**
* Custom component to render the hashtags with a custom renderer
*/
const Hashtags = withRouter(({history}) => (
    (props) => (
        <ReactHashtag
            renderHashtag={(hashtagValue) => (
                <StyledHashtag
                    onClick={history.go(`/search/${hashtagValue}`)}
                >
                    {hashtagValue}
                </StyledHashtag>
            )}
        >
            {props.children}
        </ReactHashtag>
    )
));

const Card = (props) => (
    <p>
        <Hashtags>
            {props.children}
        </Hashtags>
    </p>
);

Questions?

Feel free to fill an issue if you have any questions.