Simple yet solid solution for managing app's state via URL without covering over the rendering process.
Provides a location object and re-renders a component once URL changes.
import { Location } from "@pshershov/cocoa";
const MyComponentBasedOnRoute = () => (
<Location>
{location => (
<Employee
firstName={location.params.firstName}
lastName={location.params.lastName}
/>
)}
</Location>
);import { useLocation } from "@pshershov/cocoa";
const MyComponentBasedOnRoute = () => {
const {
params: { id }
} = useLocation();
return <MyComponent id={id} />;
};Anchor element that triggers renders across all components that uses Location.
import { Link } from "@pshershov/cocoa";
const MyComponentWithALink = () => {
return (
<Link
to="/employee"
params={{
firstName: "Kevin",
lastName: "Malone"
}}
replace={true}
>SHOW ME KEVIN<Link />
);
};Redirects to specific path every time / once the component renders
import { link } from "@pshershov/cocoa";
const handleButtonClick = () =>
link(
"/employee",
{
firstName: "Kevin",
lastName: "Malone"
},
true
);
const MyComponentThatNavigates = () => <button onClick={handleButtonClick} />;An abstraction for conditional rendering based on path
import { Match } from "@pshershov/cocoa";
const MyComponentThatMatchesPaths = () => (
<Match
path="/employee"
params={{
firstName: "Kevin",
lastName: "Malone"
}}
>
<Employee />
</Match>
);import { useMatch } from '@pshershov/cocoa'
const MyComponentThatMatchesUsingHook = () => {
const isKevinMalone = useMatch({
path: "/employee",
params: {
firstName: "Kevin",
lastName: "Malone"
}
});
return {isKevinMalone ? <Employee firstName="Kevin" lastName="Malone" /> : <NoEmployeeFound />}
}- User should manage his own rendering based on the URL
- Location will not return the familiar
Locationobject but instead a changed one with added propertie(s) likeparams(which is basically a parsedsearch) - 0 dependencies
- Most of the API will have an equivalent Component, HOC, Hook and a programmatic solution:
Location
<Location />useLocationwithLocationlocation()
Link
<Link />useLinkwithLinklink()
Match
<Match />useMatchwithMatchmatch()
