sunknudsen / react-node-to-string Goto Github PK
View Code? Open in Web Editor NEWExtract string from React node.
License: MIT License
Extract string from React node.
License: MIT License
Any chance you're thinking about updating this module to use ESM?
I'm running into an issue where react-node-to-string works fine in Vite projects but not in NextJS projects within my component library. It complains because of the default export issue of ESM and CommonJS interop.
build/index.js
has //# sourceMappingURL=index.js.map
build/index.js.map
has "sources":["../src/index.ts"]
but src/index.ts
is not included, causing:
WARNING in
../../node_modules/.pnpm/[email protected][email protected]/node_modules/react-node-to-string/build/index.js
Module Warning (from../../node_modules/.pnpm/[email protected][email protected]/node_modules/source-map-loader/dist/cjs.js
):
Failed to parse source map from'<workspace>/node_modules/.pnpm/[email protected][email protected]/node_modules/react-node-to-string/src/index.ts'
file: Error: ENOENT: no such file or directory, open'<workspace>/node_modules/.pnpm/[email protected][email protected]/node_modules/react-node-to-string/src/index.ts'
❯ tree node_modules/.pnpm/[email protected][email protected]/
node_modules/.pnpm/[email protected][email protected]/
└── node_modules
├── react -> ../../[email protected]/node_modules/react
└── react-node-to-string
├── build
│ ├── index.d.ts
│ ├── index.js
│ └── index.js.map
├── LICENCE
├── package.json
└── README.md
4 directories, 6 files
if reactNode
is a JSX in this structure
<div>
hello
{' '}
<a href="#">test</>
</div>
result would be "test"
expected result would be "hello test"
proposal fix:
"use strict"
import React from "react"
const reactNodeToString = function (reactNode: React.ReactNode): string {
let string = ""
if (typeof reactNode === "string") {
string = reactNode
} else if (typeof reactNode === "number") {
string = reactNode.toString()
} else if (reactNode instanceof Array) {
reactNode.forEach(function (child) {
string += reactNodeToString(child)
})
} else if (React.isValidElement(reactNode)) {
string += reactNodeToString(reactNode.props.children)
}
return string
}
export default reactNodeToString
placing here as I can't do a pull request :)
According to react type declaration:
type ReactFragment = Iterable<ReactNode>;
type ReactNode = ReactElement | string | number | ReactFragment | ReactPortal | boolean | null | undefined;
The ReactFragment
is an Iterable<ReactNode>
not an Array
, which is not instanceof Array
and may not contain forEach
method
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.