Comments (14)
this.shadowRoot.getElementById('draw')
should work
from lit-element.
I think, generally, it's better to use this._root
as it accounts for shadydom
, as well as shadowdom
, also you can use this.renderComplete.then
in your constructor to wait until the first render is complete.
class testElement extends LitElement {
constructor() {
super();
this.renderComplete.then(()=>{
console.log(this._root.querySelector('#draw'));
})
}
_render() {
return html`<canvas id="draw"></canvas>`;
}
}
customElements.define('test-element', testElement);
from lit-element.
Here is the full class:
class MyElement extends LitElement {
_render() {
return html`
<canvas id="draw"></canvas>
`;
}
_firstRendered() {
console.log(this.shadowRoot); // log shadow root
console.log(this.shadowRoot.getElementById('draw')); // log null
}
}
If I understand the element life cycle, firstRendered
is called after the element DOM is rendered for the first time. I run this on chrome 66.
from lit-element.
I think they should update the document :/ because when I use this.$('sampleID') it throw
'Uncaught TypeError: this.$ is not a function'
This was a Polymer-specific API that does not exist on lit-element. If you want to do this, execute querySelector
on the shadowroot or this._root
.
from lit-element.
I put the following inside _firstRendered
but the result is null
_firstRendered() {
console.log(this.shadowRoot); // log shadow root
console.log(this.shadowRoot.getElementById('draw')); // log null
}
from lit-element.
When you call this function?
from lit-element.
I tried your code and it logged me <canvas>
element as expected. Can you share the repro repository with your code?
from lit-element.
To reproduce, change the file src/components/my-view2.js
in the PWA starter kit https://github.com/Polymer/pwa-starter-kit with the following lines:
import { html } from '@polymer/lit-element';
import { PageViewElement } from './page-view-element.js';
import { SharedStyles } from './shared-styles.js';
import { connect } from 'pwa-helpers/connect-mixin.js';
import './counter-element.js';
// This element is connected to the redux store.
import { store } from '../store.js';
// These are the actions needed by this element.
import { increment, decrement } from '../actions/counter.js';
// We are lazy loading its reducer.
import counter from '../reducers/counter.js';
store.addReducers({
counter
});
class MyView2 extends connect(store)(PageViewElement) {
_render(props) {
return html`
<canvas id="draw"></canvas>
`;
}
static get properties() { return {
// This is the data from the store.
_clicks: Number,
_value: Number
}}
_firstRendered() {
console.log(this.shadowRoot.getElementById('draw'));
}
// This is called every time something is updated in the store.
_stateChanged(state) {
this._clicks = state.counter.clicks;
this._value = state.counter.value;
}
}
window.customElements.define('my-view2', MyView2);
I suspect the problem is the lazy loading mechanism in the starter kit.
from lit-element.
Just read the documentation again, maybe I should use didRender
instead ?
from lit-element.
Reading through the implementation, _firstRendered
is called only after creating the root. This means that no DOM is yet available. However, it is documented as is this would be the case. Therefore, I think this is a bug and the callback timing of _firstRendered
is wrong.
from lit-element.
@jrabary Looks like it is a bug of pwa-starter-kit
. For some reason it calls _render
after the _firstRendered
(maybe kinda of an asyncronous issue). lit-element
on the bare project works as expected: calls _render
before the _firstRendered
.
from lit-element.
@Lodin getting the child inside _didRender(props, changedProps, prevProps)
seems to be the workaround. Maybe it's the expected behavior.
from lit-element.
@jrabary I suppose, _didRender
is kinda different thing. It catches every render, not the first one, so the whole logic is compromised. I suggest you to open issue at pwa-starter-kit
, because it looks definitely as a bug.
from lit-element.
@Link2Twenty thanks
I think they should update the document :/ because when I use this.$('sampleID') it throw
'Uncaught TypeError: this.$ is not a function'
Use a native shadow DOM selector like this.shadowRoot or this._root works fine.
from lit-element.
Related Issues (20)
- Typescript error on latest build HOT 1
- mixins don't work when using ESNext in tsconfig to compile HOT 1
- Error throw when accessing queryAssignedNodes getter (No version >2.4.0) HOT 1
- Support for contexts? HOT 2
- Persistance? HOT 5
- idempotent/soft customElement decorator HOT 1
- state() is shown as deprecated in LitElement 2.5 HOT 1
- Should not be necessary to copy TemplateResult
- jsdelivr/+esm: [object Object] HOT 1
- Nested components, slot and text styling HOT 1
- Git tags are not in sync with npm tags HOT 2
- No release major/minor branches available for reference. HOT 2
- Move tests and benchmarks to GitHub Actions
- Accessibility => lit-element + Google Chrome Screen Reader Extension => UI Design is Changed HOT 3
- Property does not re-render when fired from custom event HOT 4
- Add a way to know when rendering of an element, and ALL its sub-elements, is finished HOT 3
- Litelement Boolean property returns undefined in when not set HOT 6
- Forward Compatibility With Lit 2 misleading HOT 2
- Starter projects missing in getting started documentation HOT 1
- How do you get the actual click event target? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lit-element.