npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-to-string

v0.1.1

Published

Convert React elements to strings

Downloads

11,966

Readme

React-To-String TravisCI status CircleCI

Convert React elements to strings 👌

React-To-String allows you to easily convert React elements to pure strings - no HTML tags, no react attributes - just strings.

This is useful for when you might want to toggle between eventful & styled React elements & a pure string version of those elements.

React-To-String itself has 0 dependencies and has a minimized bundle size of 1kb. ⚡

You can use React-To-String to easily convert complex react-elements like:

<div>
  <p>
    Feeling <a href="/tired">tired</a>? Need some <a href="/food">food</a>?
    { ' ' }
    <button>Try React-To-String!</button>
    { ' ' }
    <span className="small-text">
      Disclaimer: This library only converts React elements to strings.
    </span>
  </p>
</div>

to

Feeling tired? Need some food? Try React-To-String! Disclaimer: This library only converts React elements to strings.

Installation

To install, use your package manager to install react-to-string:

npm install --save react-to-string

or

yarn add react-to-string

Usage

import React, { Component } from 'react';
import reactToString from 'react-to-string';

class App extends Component {
  render() {
    return <div>Hello world!</div>
  }
}

reactToString(<App />);
// Returns "Hello world!"

reactToString(<p>Are you interested in this library? <a href="signup">Click here!</a></p>);
// Returns "Are you interested in this library? Click here!"

Note: This example uses ES6 syntax, if you are using ES5 require syntax, make sure to require react-to-string as a ES6 module like so:

var reactToString = require('react-to-string').default

API

reactToString(element: React element || String || Array (Strings or ReactElements)) : Function => String

Takes in a React element and traverses its children to return a pure string.

Note: react-to-string always returns a string. If an undefined or some other unknown object is passed is provided as an argument, react-to-string will return an empty string.

Development

React-To-String uses jest to handle tests. To develop React-To-String locally:

  1. Clone this repo and cd into the newly cloned folder.
  2. Install dev dependencies by running npm install or yarnRun npm test or yarn test to run the test suite.
  3. Run npm test or yarn test to run the test suite.

Looking for server-side rendering or string output with HTML tags?

React-To-String won't be able to do that for you, but you can find more about server-side rendering or string output with HTML tags (static markup) with React at the official docs here: https://facebook.github.io/react/docs/react-dom-server.html