question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stand-alone, cross-component functions?

See original GitHub issue

Newbie to Preact here ✨ Love how fast it is.

Is it possible to share stand-alone functions across various components? Several components in my preact app share functions that are unrelated to any particular components.

For example (I custom-built my own router):

routes/index.js

export const testForUrlRedirect = ({ url, props }) => {
  ...
  return newUrl
}
export const getPageDataFromPageIdOrUrl = ({ pageId, url }) => {
  ...
  return pageData
}

MyComponent.js

import { testForUrlRedirect, getPageDataFromPageIdOrUrl } from '../routes'

In MyComponent, neither functions work as expected.

I did notice #722, yet that issue refers to functions that are part of other components. Is it possible to have stand-alone functions and share them across various components?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JoviDeCroockcommented, Jul 23, 2019

in general, routes/*.js and routes/*/index.js must export single components, since they get auto-wrapped by the CLI for lazy loading if you have helper functions you’ll need to move them so they don’t match those patterns behind the scenes, when you do import Foo from 'routes/foo', you’re basically getting: const Foo = lazy(() => import('routes/foo'))

A bit of a more fitting explanation

0reactions
heymartinadamscommented, Jul 22, 2019

Thanks @JoviDeCroock 👌

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting started with standalone components - Angular
Standalone components provide a simplified way to build Angular applications. Standalone components, directives, and pipes aim to streamline the authoring ...
Read more >
React Function Components
Everything you need to know about React Function Components -- also known as React Functional Components. Here you will learn React Function ......
Read more >
Angular — Four Ways for Communication Between Components
Instead of establishing a direct linkage between components, a standalone injectable (service) is used as a middleman between them.
Read more >
What is a Standalone Component in Angular - YouTube
GitHub Linkhttps://github.com/TheDotNetOffice/Angular-15-Tutorial-Classhttp://www.dotnetoffice.com/Support ...
Read more >
Angular2 - call a function from another standalone component ...
I'm loading in a component (Component1) another standalone component(Component2) that contains a form. Imagine you have made a form component ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found