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.

Creating a side-effect free `new Component`

See original GitHub issue

Is your feature request related to a problem? Please describe. When using a component in a JavaScript file, you do it like this:

import Component from `MyComponent.svelte`;

const myComponent = new Component({
   target: document.getElementById('main')
});

In our project we have the eslint rules no-new and no-unused-vars which get triggered by this code: The code triggers no-unused-vars, but if I remove const myComponent = , the code triggers the no-new rule.

Describe the solution you’d like I’d like the component to be able to do a “delayed mount”, to have a side-effect-free constructor if it does not receive a target property (for ease of use you can leave the default behavior like it is). Taking a peek at Vue, I’d like a syntax like this:

const myComponent = new Component();
myComponent.mount(document.getElementById('main'));

Describe alternatives you’ve considered For now, I’ll add a

// eslint-disable-next-line no-new
new Component({
   target: document.getElementById('main')
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Oct 4, 2019

Assuming this issue is motivated by a desire to appease the linter without disabling the line, and you don’t actually need a separate mount function, an alterntive for you could be like this, for example:

(() => new App({
  target: document.getElementById('app'),
  hydrate: true,
  props: {}
}))()
1reaction
sw-yxcommented, Oct 1, 2019

not sure i understand. your motivations seem entirely because of no-new. yet your proposed syntax still includes new. why do you really want a delayed mount? your solution doesnt fit your stated problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

65 Handling side effect in class component - YouTube
In this lecture you will learn, how to handle side effects in a class based component and what are lifecycle methods of a...
Read more >
How to make a class-based custom element side-effect-free ...
From webpack guide - Mark the file as side-effect-free: All the code noted above does not contain side effects, so we can simply...
Read more >
Side-effects in Compose - Android Developers
A side-effect is a change to the state of the app that happens outside the scope of a composable function. Due to composables'...
Read more >
Effectful (But Side-Effect Free) Programming - pfun
Effectful (But Side-Effect Free) Programming. In functional programming, programs are built by composing functions that have no side-effects.
Read more >
4 Working with side effects - React Hooks in Action
Recognizing types of side effects in components; Wrapping side effects with the useEffect hook; Controlling when an effect runs by specifying a dependency ......
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