
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
@josiahayres/react-hooks
Advanced tools
react-hooks is a collection of React Hooks, written in Typescript.
Use the package manager npm to install @josiahayres/react-hooks.
npm install @josiahayres/react-hooks
When you have forms with multiple sections, this hook helps by enforcing the following rules:
You are responsible for ensuring each Section must control
import { useFormSectionsControl } from 'josiahayres/react-hooks';
// Create a Union type with all valid section IDs
type ValidFormSectionIds =
| 'sectionOne'
| 'sectionTwo'
| 'sectionThree'
| 'sectionFour';
// This form uses three sections.
const sectionIds = ['sectionOne', 'sectionTwo', 'sectionThree'];
// Initialize hook in component
const formSectionsControl =
useFormSectionControl<ValidFormSectionIds>(sectionIds);
This is what the hook returns. Is a tuple with the following:
const [store, dispatch, canEditSection] = formSectionsControl;
Also accessable as formSectionsControl[0]
.
// Store object can be destructured like this
const { activeSectionId, haveVisitedSummary, formSections, options } = store;
Store | Notes |
---|---|
activeSectionId | One of provided formSections or null |
haveVisitedSummary | True once dispatch({type:"gotToNextSection"}) called when activeSectionId is last id in provided list |
formSections | Same as provided list of formSections, see section below for more |
options | Same as provided options object, see section below for more |
This is the options object as provided to the hook on setup. All options in this object are optional.
Key | When key has value: | Notes |
---|---|---|
initialActiveSectionId | null | haveVisitedSummary=true, activeSectionId=null |
initialActiveSectionId | "sectionOne" | haveVisitedSummary=false, activeSectionId="sectionOne" |
initialActiveSectionId | Not provided | haveVisitedSummary=false, activeSectionId=first section in provided list |
Also accessable as formSectionsControl[1]({type:""})
.
Action Type | Action Parameters | Notes |
---|---|---|
reset | None | Will reset internal hook state to value at initial render |
goToNextSection | None | Will step through each section in list provided, until no more sections are active. |
goTo | sectionId | Sets a specific sectionId active |
Examples:
// Typescript will help dispatching the correct actions
dispatch({ type: 'goToNextSection' });
dispatch({ type: 'goTo', sectionId: 'sectionTwo' });
dispatch({ type: 'reset' });
Function takes a sectionId and returns if that sectionId can be edited. These checks are used internally, in this order.
null
return truenull
return falseconst sectionOneEditable = canEditSection('sectionOne');
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
FAQs
React Hooks
We found that @josiahayres/react-hooks 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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.