Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
@svgr/plugin-jsx
Advanced tools
@svgr/plugin-jsx is a plugin for SVGR (SVG to React) that transforms SVG files into JSX code. This allows developers to use SVGs as React components, making it easier to integrate and manipulate SVG graphics within React applications.
Basic SVG to JSX Transformation
Transforms a basic SVG string into JSX code that can be used as a React component.
const svgCode = '<svg><circle cx="50" cy="50" r="40" /></svg>';
const jsxCode = transform(svgCode, { plugins: ['@svgr/plugin-jsx'] });
console.log(jsxCode);
Customizing JSX Output
Allows customization of the JSX output using Babel presets and other configurations.
const svgCode = '<svg><circle cx="50" cy="50" r="40" /></svg>';
const jsxCode = transform(svgCode, { plugins: ['@svgr/plugin-jsx'], jsx: { babelConfig: { presets: ['@babel/preset-react'] } } });
console.log(jsxCode);
Adding Props to SVG Components
Automatically adds props to the SVG components, making them more flexible and reusable.
const svgCode = '<svg><circle cx="50" cy="50" r="40" /></svg>';
const jsxCode = transform(svgCode, { plugins: ['@svgr/plugin-jsx'], jsx: { expandProps: 'start' } });
console.log(jsxCode);
react-svg is a library that allows you to import SVG files as React components. It provides similar functionality to @svgr/plugin-jsx but focuses more on runtime usage rather than build-time transformations.
svg-react-loader is a Webpack loader that transforms SVG files into React components. It offers similar capabilities to @svgr/plugin-jsx but is specifically designed to work within the Webpack ecosystem.
react-inlinesvg is a React component that loads and injects SVGs inline. Unlike @svgr/plugin-jsx, it focuses on runtime loading and rendering of SVGs rather than transforming them into JSX at build time.
Transforms SVG into JSX.
npm install --save-dev @svgr/plugin-jsx
.svgrrc
{
"plugins": ["@svgr/plugin-jsx"]
}
@svgr/plugin-jsx
consists in three phases:
@svgr/babel-preset
transformationsYou can extend the Babel config applied in this plugin using jsx.babelConfig
config path:
// .svgrrc.js
module.exports = {
jsx: {
babelConfig: {
plugins: [
// For an example, this plugin will remove "id" attribute from "svg" tag
[
'@svgr/babel-plugin-remove-jsx-attribute',
{
elements: ['svg'],
attributes: ['id'],
},
],
],
},
},
}
Several Babel plugins are available:
@svgr/babel-plugin-add-jsx-attribute
@svgr/babel-plugin-remove-jsx-attribute
@svgr/babel-plugin-remove-jsx-empty-expression
@svgr/babel-plugin-replace-jsx-attribute-value
@svgr/babel-plugin-svg-dynamic-title
@svgr/babel-plugin-svg-em-dimensions
@svgr/babel-plugin-transform-react-native-svg
@svgr/babel-plugin-transform-svg-component
If you want to create your own, reading Babel Handbook is a good start!
MIT
FAQs
Transform SVG into JSX
We found that @svgr/plugin-jsx demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.