Conditional Rendering of Contents of Navbar

I am trying to conditionally render different parts of my navbar to the user depending on if they are logged in or not. I initially achieved this by using the onMount function, but it is not ideal as there is a slight delay after the page loads before the right navbar contents are shown. I could fix this by adding a loading indicator, but I don’t want to do that.

So, I want to get the response from the server and then render out the right navbar to the user as soon as the page loads. I used the load function in my +layout.js as shown below –

export async function load({ fetch }) {
    try {
        console.log('Fetching admin status from API...');

        const response = await fetch('http://localhost:8000/check-admin-status/', {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            },
            credentials: 'include',
        });

        console.log('Response Status:', response.status); // Log the response status
        const data = await response.json();

        console.log('Fetched Data:', data); // Log the fetched data

        return {
            isAdmin: data.is_admin,
            isAuthenticated: data.is_authenticated,
        };
    } catch (error) {
        console.error('Error fetching admin status:', error);
        return {
            isAdmin: false,
            isAuthenticated: false,
        };
    }
}

This successfully returns the server’s response –

Fetching admin status from API...
+layout.js:13 Response Status: 200
+layout.js:16 Fetched Data: {{is_admin: false, is_authenticated: true}
is_admin
: 
false
is_authenticated
: 
true

But how do I inform my Navbar of this response?

in my +page.svelte, which is in the same directory as my +layout.js I have –

<script>
  import Navbar from "../components/Navbar.svelte";
  import Modal from "../components/Modal.svelte";

  export let isAdmin;
  export let isAuthenticated;

  // Log the props to verify they are received correctly
  console.log('Page Props - isAdmin:', isAdmin);
  console.log('Page Props - isAuthenticated:', isAuthenticated);
</script>

<Navbar {isAdmin} {isAuthenticated}/>

<Modal modalId="exampleModal" modalTitle="Create an account to like or comment." />

But the log statements come out as undefined

What I want to do is get the server’s response in my +layout.js (which I have successfully gotten) > pass it to the +page.svelte, and then further pass it on to my Navbar component so that the appropriate parts of the Navbar would be rendered to the user as the page loads.

my folder structure is –

src > routes > +page.svelte and +layout.js

it is my homepage

this is my Navbar.svelte –

<script>
export let isAdmin;
export let isAuthenticated;
</script>

<!-- preceding html -->
      <ul class="navbar-nav ms-auto d-flex align-items-center">
        {#if isAdmin}
          <li class="nav-item">
            <a class="nav-link d-flex align-items-center me-3" href="#" data-bs-toggle="modal" data-bs-target="#exampleModal">
              <i class="bi bi-pencil-square"></i> Write
            </a>
          </li>
        {/if}
        {#if isAuthenticated}
          <li class="nav-item">
            <a class="nav-link" href="#">
              <i class="bi bi-bell"></i> 
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/profile">
              <i class="bi bi-person-circle profile-icon"></i>
            </a>
          </li>
        {:else}
          <li class="nav-item">
            <button class="btn sign-up me-3" data-bs-toggle="modal" data-bs-target="#exampleModal">Sign up</button>
          </li>
          <li class="nav-item">
            <a class="nav-link me-3" href="/signin">Sign in</a>
          </li>
        {/if}
      </ul>

What to do?

Pages and layout do not accept arbitrary properties, all data from load functions is passed to a property called data. So the values will be on data.isAdmin & data.isAuthenticated.

See the data loading docs.

You also do not have to pass load data down, you can access it via the page store, so in Navbar you can just use $page.data.isAdmin directly.

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