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.
use-settings
Advanced tools
Readme
Easily manage application settings in localStorage with a React hook.
toggleSetting
yarn add use-settings
or
npm install use-settings
First, you need to wrap any consuming components with the SettingsProvider
. We use this so that you can set a global localStorage
key (among other things).
import { SettingsProvider } from 'use-settings';
const App = () => {
return (
<SettingsProvider name='my-settings-key' initial={{
color: 'cyan'
}}>
// the rest of your components
</SettingsProvider>
);
}
Now, just import the useSettings
hook in any component that needs it:
import useSettings from 'use-settings';
const MyComponent = () => {
const { settings, updateSetting } = useSettings();
// ...
}
This section will explain the provider and the hook and its exported values.
SettingsProvider
We don't recommend wrapping your entire <App />
directly with the SettingsProvider
. Even though the props you pass into it probably won't change, we don't want to re-render the entire app unnecessarily. Therfore, keep the SettingsProvider
as close to the components that need the hook as possible. That is, if your navigation bar is the only component that needs the hook, wrap it with the provider, not anything above it.
The provider accepts two props: name
and initial
name
This is the name (or key value) of the settings object in localStorage
. This sets the value globally.
initial
This is the initial value of the settings object. It's used in the event that the existing settings aren't found in localStorage
and in the reset
function explained later.
useSettings
All of the values returned from the hook can be seen below:
const {
settings,
updateSetting,
toggleSetting,
reset
} = useSettings();
settings
The current settings object. You can access items with dot notation or by the object key pattern:
console.log(settings.color);
or
console.log(settings['color']);
Note: Do not try to update a value directly. Remember, state should never be mutated directly in React. It may seem to work, but you'll run into problems later.
updateSettings
Allows you to modify an arbitrary amount of settings in object format.
updateSettings({
color: 'cyan',
index: 100
});
The resulting object is merged with the one you provide. Therefore, if your settings object looks like:
{
dark: true,
color: 'green',
index: 1000,
}
and you call updateSettings
with:
updateSettings({
color: 'cyan',
index: 100
});
then the dark
key in the settings is not overwritten.
updateSetting
A function that modifies a single setting value.
updateSetting('color', 'cyan');
toggleSetting
A function that toggles a setting between true
and false
. If no value is found (if it doesn't exist in the settings object yet), it'll default to true
initially.
toggleSetting('dark');
reset
A function that overwrites the existing settings object with the value you provide. If you don't provide a value, it defaults to the initial
object you gave the SettingsProvider
reset({
color: 'green'
});
or simply:
reset();
FAQs
Easily manage application settings in localStorage with a React hook.
The npm package use-settings receives a total of 0 weekly downloads. As such, use-settings popularity was classified as not popular.
We found that use-settings 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.