Get-started
React-stillness-component is a npm packages.
info
Read Intro to get a brief idea of how it works!
Install
Recommended use of yarn to manage npm dependencies
- yarn
- npm
- pnpm
yarn add react-stillness-component
npm install react-stillness-component
pnpm add react-stillness-component
usage
The component makes extensive use of Hooks
, so it is necessary to introduce React 16.8+
Introduce
<StillnessProvider>
to your project and try to place it at the top levelindex.jsimport React from 'react';
import ReactDOM from 'react-dom';
import { StillnessProvider } from 'react-stillness-component';
import App from './App';
ReactDOM.render(
<StillnessProvider>
<App />
</StillnessProvider>,
document.getElementById('root') );Use
<Offscreen>
to wrap the components you need to be stillness...
function App() {
const [show, setShow] = useState(true)
return (
<div>
<button onClick={() => setShow(show => !show)}>Toggle</button>
<Offscreen visible={show}>
<Count />
</Offscreen>
</div>
);
}
...Stillness-component control, using
useStillness
orconnectStillness
to listen for relevant stillness behavior and to manually control the stillness stateimport { Offscreen,useStillness } from 'react-stillness-component';
function Count() {
const [count, setCount] = useState(0);
const collected = useStillness({
mounted: (contract) => {
return 'mounted';
},
unmounted: (contract) => {
return 'unmounted';
},
collect: (contract) => {
return {
isStillness: contract.isStillness(),
stillnessId: contract.getStillnessId(),
unset: contract.unset, // function
clear: contract.clear, // function
};
},
});
return (
<div className="count">
<p>{collected.isStillness}</p>
<p>class count: {count}</p>
<button
onClick={() => {
setCount((count) => count + 1);
}}
>
Add
</button>
</div>
);
}
For more information, please see the API documentation
TypeScript support
This project was developed using the TypeScript language and provides a full definition with the ability to preview component property input suggestions and definition checks