Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
react-lazy-props
Advanced tools
Readme
LazyLoad media elements with react-lazy-props
and IntersectionObserver
npm i react-lazy-props
Wrap the elements you want to lazyload with LazyProps
component
It will unload media before components were mounted and will load them only when they are observed.
import LazyProps from "react-lazy-props";
...
return (
<LazyProps>
<div>
<h2 style={{backgroundImage: `url("${backgroundImage}")`}}>Title</h2>
<p>Some Images</p>
<img src="example.jpg" srcSet={"example2x.jpg 2x", "example3x.jpg 3x"} />
<video src="https://www.example.com/video.ogg" />
</div>
</LazyProps>
);
LazyProps
renders its child components with props and styles replaced according to the map:
src
to data-src
srcSet
to data-srcset
backgroundImage
to data-bg-src
<div>
<h2 data-bg-src="url('background-image.png')">Title</h2>
<p>Some Images</p>
<img data-src="example.jpg" data-srcset="example2x.jpg 2x, example3x.jpg 3x" />
<video data-src="https://www.example.com/video.ogg" />
</div>
LazyProps
component observes NodeList with Unloaded Prop Keys using IntersectionObserverAPI
.
As soon as at least 1/10 of Node element is intersected, element props will gain thier initial key.
<div>
<h2 style="background-image: url('background-image.png')">Title</h2>
<p>Some Images</p>
<img src="example.jpg" srcset="example2x.jpg 2x, example3x.jpg 3x" />
<video src="https://www.example.com/video.ogg" />
</div>
NOTE! Always wrap child components as LazyProps
component returns its children unwrapped.
Arguments:
element
- Node ObjectFunction will be invoked when one of the child elements is intersected.
import LazyProps from "react-lazy-props";
...
doSomething(element){
element.classList.add("intersected");
}
}
...
render {
return (
<LazyProps onElementLoad={element => this.doSomething(element)}>
...
Arguments:
props
- ObjectcomponentType
- StringFunction will be invoked after component props were unloaded, this way you will be able to customize unloaded props of the element, for example setup a placeholder src to images:
import LazyProps from "react-lazy-props";
...
setPlaceholder(props, componentType){
if(componentType === "img"){
props.src = this.state.placeholder;
}
}
return props;
}
...
render {
return (
<LazyProps onUnloadProps={(props, componentType)=> this.setPlaceholder(props, componentType)}>
...
NOTE! The function must always return props object.
Default: false
If set to true elements wont be unloaded, but observer still will be there, this approach will be more efficient because we will not have to loop through child elements and components unloading their props.
The differecne is that you will have to set lazy props yourself:
import LazyProps from "react-lazy-props";
...
return (
<LazyProps unloaded={true}>
<div>
<h2 data-bg-src={backgroundImage}>Title</h2>
<p>Some Images</p>
<img data-src="example.jpg" />
<video data-src="https://www.example.com/video.ogg" />
</div>
</LazyProps>
);
Since this package is observing elements with IntersectionObserver
, you may want old browsers to support it with IntersectionObserver polyfill
FAQs
Lazyload component props with IntersectionObserver API
The npm package react-lazy-props receives a total of 4 weekly downloads. As such, react-lazy-props popularity was classified as not popular.
We found that react-lazy-props 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.