Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
forgo-router
Advanced tools
A simple router for Forgo. Just about 1KB gzipped.
npm i forgo-router
It's fairly straight forward to use:
Both functions take a callback as the second argument, which gets called if the match is a success.
Here's an example:
import { Router, matchExactUrl, matchUrl } from "forgo-router";
function App() {
return {
render() {
return (
<Router>
<Link href="/">Go to Home Page</Link>
{matchExactUrl("/", () => <Home />) ||
matchUrl("/customers", () => <Customers />) ||
matchUrl("/about", () => <AboutPage />)}
</Router>
);
},
};
}
Sometimes you'd want to extract a parameter from a url, such as "103" from /customers/103. You can do that by defining a parameter in the pattern.
Your url pattern would now look like "/customers/:id", where id is the name of the captured parameter. The captured parameter is avalable in the callback, and can now be passed to the component to be rendered.
function Customers() {
return {
render() {
return (
<div>
<h1>Customers</h1>
<div>
{matchExactUrl("/customers", () => (
<CustomerList customers={customers} />
)) ||
matchExactUrl("/customers/:id", (match) => (
<CustomerDetails id={match.params.id} />
))}
</div>
</div>
);
},
};
}
The Link component creates an anchor tag which can be used to navigate to a certain url. The reason to not use regular anchor tags (A tags) is that they will refresh the page - while the Link component merely changes the url via pushState APIs.
function Home() {
return {
render() {
return (
<div>
<h1>Home Page</h1>
<p>Welcome to Forgo Examples Inc.</p>
<ul>
<li>
Go to <Link href="/customers">Customers</Link>
</li>
<li>
Go to the <Link href="/about">About Page</Link>
</li>
</ul>
</div>
);
},
};
}
The callback used with matchUrl and matchExactUrl is of the type given below:
type MatchResult = {
matchedPath: string;
params: { [key: string]: string };
remainingPath: string;
};
Here's a more complete example. You can try it on CodeSandbox.
import { mount } from "forgo";
import { Router, matchUrl, matchExactUrl, Link } from "forgo-router";
type Customer = {
id: number;
name: string;
age: number;
};
const customers = [
{ id: 1, name: "Kai", age: 3 },
{ id: 2, name: "Jeswin", age: 40 },
{ id: 3, name: "Deepsta", age: 42 },
];
function App() {
return {
render() {
return (
<Router>
<Link href="/">Home Page</Link>
{matchExactUrl("/", () => <Home />) ||
matchUrl("/customers", (match) => <Customers />) ||
matchUrl("/about", () => <AboutPage />)}
</Router>
);
},
};
}
function Home() {
return {
render() {
return (
<div>
<h1>Home Page</h1>
<p>Welcome to Forgo Examples Inc.</p>
<ul>
<li>
Go to <Link href="/customers">Customers</Link>
</li>
<li>
Go to the <Link href="/about">About Page</Link>
</li>
</ul>
</div>
);
},
};
}
function Customers() {
return {
render() {
return (
<div>
<h1>Customers Module</h1>
<div>
{matchExactUrl("/customers", () => (
<CustomerList customers={customers} />
)) ||
matchExactUrl("/customers/:id", (match) => (
<CustomerDetails id={match.params.id} />
))}
</div>
</div>
);
},
};
}
type CustomersProps = {
customers: Customer[];
};
export function CustomerList(props: CustomersProps) {
return {
render(props: CustomersProps) {
return (
<div>
<h2>List of Customers</h2>
<ul>
{props.customers.map((c) => (
<li>
<Link href={`/customers/${c.id}`}>
{c.name}({c.age})
</Link>
</li>
))}
</ul>
</div>
);
},
};
}
type CustomerDetailsProps = {
id: string;
};
export function CustomerDetails(props: CustomerDetailsProps) {
return {
render(props: CustomerDetailsProps) {
const customer = customers.find((c) => c.id.toString() === props.id);
return (
<div>
<h2>Customer Details</h2>
{customer ? (
<p>
Details for {customer.name}. Id: {customer.id}, Age:{" "}
{customer.age}
</p>
) : (
<p>Missing customer.</p>
)}
</div>
);
},
};
}
export function AboutPage() {
return {
render() {
return (
<div>
<h1>About Page</h1>
<p>Hello, world</p>
</div>
);
},
};
}
function ready(fn: any) {
if (document.readyState != "loading") {
fn();
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
ready(() => {
mount(<App />, document.getElementById("root"));
});
FAQs
A simple router for Forgo. Just about 1KB gzipped.
We found that forgo-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.