Child component failed to update from parent ref

I’m trying to update a chain of components from side class by passing a callback to function with a ref variable update (component with a class to listen to websocket).

After the callback is called, i receive an warning Invalid prop: type check failed for prop "teamInfo". Expected Object, got Undefined and all of child components are failed because of undefined data was passed into them.

I tried to update ref from same component by making a button and calling a function that will do the same thing as a class with callback, but the data is successfully updated.

Minimal code sample to test:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script setup lang="ts">
import { ref } from "vue";
import {HubConnectionBuilder} from "@microsoft/signalr";
enum DecryptoTeam {
Unknown = 0,
Red = 1,
Blue = 2
}
interface DecryptoMatchFake {
round: number;
teams: { [key in DecryptoTeam]: TeamFake };
}
interface TeamFake {
players: { [key: string]: string };
}
let loading = ref<boolean>(true);
let matchInfo = ref<DecryptoMatchFake | null>(null);
const props = defineProps<{ id: string }>();
const connection = new HubConnectionBuilder()
.withUrl("/api/decryptoMessageHub")
.withAutomaticReconnect()
.build();
function start() {
try {
connection.start().then(() => {
connection.invoke("JoinMatch", props.id);
connection.on("StateChanged", (match: DecryptoMatchFake) => {
matchInfo.value = match;
loading.value = false;
});
});
} catch (err) {
console.error(err);
setTimeout(start, 5000);
}
}
start();
</script>
<template>
<div v-if="loading" class="title">
Loading match data...
</div>
<div v-if="matchInfo" class="content">
<p class="title">Round #{{matchInfo.round}}</p>
<div class="split">
<div class="team">
<div class="block-center">
<p v-for="player in matchInfo.teams[DecryptoTeam.Blue].players">{{player}}</p>
</div>
</div>
</div>
</div>
</template>
</code>
<code><script setup lang="ts"> import { ref } from "vue"; import {HubConnectionBuilder} from "@microsoft/signalr"; enum DecryptoTeam { Unknown = 0, Red = 1, Blue = 2 } interface DecryptoMatchFake { round: number; teams: { [key in DecryptoTeam]: TeamFake }; } interface TeamFake { players: { [key: string]: string }; } let loading = ref<boolean>(true); let matchInfo = ref<DecryptoMatchFake | null>(null); const props = defineProps<{ id: string }>(); const connection = new HubConnectionBuilder() .withUrl("/api/decryptoMessageHub") .withAutomaticReconnect() .build(); function start() { try { connection.start().then(() => { connection.invoke("JoinMatch", props.id); connection.on("StateChanged", (match: DecryptoMatchFake) => { matchInfo.value = match; loading.value = false; }); }); } catch (err) { console.error(err); setTimeout(start, 5000); } } start(); </script> <template> <div v-if="loading" class="title"> Loading match data... </div> <div v-if="matchInfo" class="content"> <p class="title">Round #{{matchInfo.round}}</p> <div class="split"> <div class="team"> <div class="block-center"> <p v-for="player in matchInfo.teams[DecryptoTeam.Blue].players">{{player}}</p> </div> </div> </div> </div> </template> </code>
<script setup lang="ts">
import { ref } from "vue";
import {HubConnectionBuilder} from "@microsoft/signalr";

enum DecryptoTeam {
  Unknown = 0,
  Red = 1,
  Blue = 2
}

interface DecryptoMatchFake {
  round: number;
  teams: { [key in DecryptoTeam]: TeamFake };
}
interface TeamFake {
  players:               { [key: string]: string };
}

let loading = ref<boolean>(true);
let matchInfo = ref<DecryptoMatchFake | null>(null);
const props = defineProps<{ id: string }>();

const connection = new HubConnectionBuilder()
    .withUrl("/api/decryptoMessageHub")
    .withAutomaticReconnect()
    .build();

function start() {
  try {
    connection.start().then(() => {
      connection.invoke("JoinMatch", props.id);
      connection.on("StateChanged", (match: DecryptoMatchFake) => {
        matchInfo.value = match;
        loading.value = false;
      });
    });
  } catch (err) {
    console.error(err);
    setTimeout(start, 5000);
  }
}

start();
</script>

<template>
  <div v-if="loading" class="title">
    Loading match data...
  </div>
  <div v-if="matchInfo" class="content">
    <p class="title">Round #{{matchInfo.round}}</p>
    <div class="split">
      <div class="team">
        <div class="block-center">
          <p v-for="player in matchInfo.teams[DecryptoTeam.Blue].players">{{player}}</p>
        </div>
      </div>
    </div>
  </div>
</template>

Full source code is available here.

8

In your TeamInfo.vue file you are updating matchInfo onMounted but this lifecycle hook is only called after the template has already compiled (see vue lifecycle hooks).

the onMounted hook can be used to run code after the component has finished the initial rendering and created the DOM nodes

Your components are being passed props which are still null. I would try moving your DecryptoDataService.getMatch(props.id) promise into script setup – that way it gets run during setup rather than after the template is compiled. You’ll probably be good to just completely remove the onMounted hook.

2

I found the issue. It was a matter of a wrong serialization of dictionary keys on a backend side. I fixed it with a custom serializer for a dictionary keys, but haven’t thought that SignalR will have the same problem.

Before:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{... teams: {'Blue': {}, 'Red': {}} }
</code>
<code>{... teams: {'Blue': {}, 'Red': {}} } </code>
{... teams: {'Blue': {}, 'Red': {}} }

After:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{... teams: {1: {}, 2: {}} }
</code>
<code>{... teams: {1: {}, 2: {}} } </code>
{... teams: {1: {}, 2: {}} }

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật