livewire: initial-data attribute not found in component after updating to 1.0.1
I updated livewire from 0.7.4 to 1.0.1 and after updating I get this error in the console.
index.js:19 Uncaught TypeError: Cannot read property 'data' of null
at new Component (index.js:19)
at index.js:69
at Array.forEach (<anonymous>)
at Livewire.start (index.js:67)
at HTMLDocument.<anonymous> (dashboard:38)
The attribute is in the rendered HTML but when the component is parsed by livewire, it is not found.
Update:-
I dug into the issue a little bit morning. Here are the steps to reproduce the issue. I assume that you have a fresh installation of Laravel with livewire and Turbolinks installed and configured.
- Create two separate livewire components.
- Include those two livewire components in two separate pages.
- Now add links into your layout to navigate between the two pages.
- Now navigate to any one of the pages and then quickly navigate to another page while the previous page request is still pending.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 12
- Comments: 60 (8 by maintainers)
Got the same error when using nested components and not using Turbolinks.
Fixed the issue by changing the nested component’s key to a random one.
Before
After
And still don’t know why this tweak works 🤔
Seeing this too in my app.
Update: Found the problem. Disabling the
options.views.dataDataCollector in debugbar’s config resolves the issue for me. It appends multiple instances of the components’ rendered views to the DOM thus causing livewire to re-evaluate those, too. Eventually theComponentconstructor is run more than once.Derived from that i guess that the problem boils down to a component with the same
wire:idbeing in the DOM multiple times - regardless how it got there.I get this error as soon as i add any component to a view, an “empty” one, too (v1.0.12). Logging
console.log('initialize', this.id, initialData);in theComponentconstructor:With just one component on the page, it is constructed multiple times, deleting the
initial-dataattribute after the first time (present on initial load)Subsequent instantiations will cause said error as the attribute is not there anymore, obviously. Question is why the constructor is called that often ~, what removing the attribute was added for. Going by the commit date that error should’ve come up much earlier; something in the lifecycle of components changed recently?~
Not too deep into the code yet, just started using it yesterday. (Love livewire btw - such a treat to work with)
Agreed @indigoram89
It gets solved also by adding a
keyto every component. Not only to components rendered in loops. 👍As this seems like a very common scenario, sub-component inside a foreach, this probably needs to be looked at.
I just ran into the same issue with a simple list of posts, using a ‘post’ sub-component. Pagination will only work for the first click.
Using the bulit-in Laravel pagination without the ‘WithPagination’ trait, it works fine as well.
Any ideas @calebporzio?
Having the same issue … not sure how to provide more detail. Here is what I’m seeing:
I have encountered this this error when having a laravel component inside livewire component template that had a livewire component as a slot. Adding
keydidn’t solve it for me.Just stumbled into this problem. Got it solved putting :keys to every nested component in a loop. Hopefully it helps!
I just ran into this, using nested components inside a foreach:
The error appears when the list is updated from the
wire.pollAdding a unique key to the nested component did the trick for me:
Adding the
keyattribute is not a big deal, although it is not a very obvious solution; maybe just adding a more verbose error message could help.My issue (above) was resolved by adding https://github.com/livewire/vue. (I’ve updated my repository with the fixed version in this branch)
For my personal project I also needed to add
window.vue = Vueinapp.jsas described hereHello everyone! Want to share my two cents on how to fix these kind of bugs. I’ve had some eureka moments when working with Livewire. It’s not a vast list and it’s narrowed down to my experience with the framework.
keyattribute when rendering components in a loop. Not:keyorwire:key. Justkey. This key must be unique among the rendered components.key. However, they can break when rendering next to other dynamics parts of the view. For exampleThis will break the nested component when the foreach renders a new element. My wild guess is that Livewire tracks the position in the DOM where the component is located. Let’s say we have 10 items rendered, then our NestedComponent will be on the 11th position. When rendering 11 items, our component goes to the 12th position and it breaks. To avoid this, add a wrapper element to every dynamic sections of your view: foreach, if, etc.
This time, the Nested component will be always in the 2nd position, no matter how many the items the loop renders.
Hope this helps!
Ok I’ve also done some digging, this is with nested components inside of a
@foreach:Once I removed the sub-component, I didn’t get this issue anymore…
Adding the key solves the problem for me:
@livewire(“path.to.component.{$model->id}”, [], key($model->id))
Okay, I’ve figured it out!
So, there are two noteworthy things here:
Previously, we needed to add this code to get Turbolinks and Livewire to work nicely together:
Now, we don’t have to do that:
What was happening:
window.livewire.start()was being called in my code and in Livewire’s code. Calling this twice led to a bunch of code getting re-run twice, includingthis.extractLivewireAttribute('initial-data'). The attribute gets removed the first timestart()is called, and fails the second time.Boom!
Thank you! @asantibanez
First, I’ll say that sometimes when you see this error, it’s because you are loading
@livewireScriptstwice.I added a console.warn to Livewire to help people with this case.
It appears there’s something deeper here with nested components.
I would love for someone to post an explicit replication of this error that I can copy and past requiring no domain code. Otherwise, it’ll be very hard for me to debug.
Thanks!
Well if you use
It will behave the same as:
Because livewire will add a random string as key if there is no key defined.
Now work, with key(rand()) only not, very stranger…
Thank @heruputra
According to this document
After a long search, This one works for me.
Is there a temporary solution to this issue until it gets resolved?
Sorry, will need more info on the problem.