useNavigate() isn’t working in my Remix Shopify app

I’m building a Shopify Remix app and am having trouble getting navigation to work when using the useNavigate() Remix hook. I have two Polaris components attempting to use the hook in the code below (the EmptyState and Page components) and when I click their respective buttons in the UI, nothing happens. I’ve tried adding console.logs within the onAction functions and nothing is showing up in the console either when I click. Does anyone have any ideas?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import {
Card,
Layout,
Page,
Text,
IndexTable,
Thumbnail,
useIndexResourceState,
Badge,
EmptyState,
} from "@shopify/polaris";
import { useLoaderData, useNavigate, Link } from "@remix-run/react";
import { json } from "@remix-run/node";
import { authenticate } from "~/shopify.server";
import { DeleteIcon } from "@shopify/polaris-icons";
import { QUERY_PAGES } from "~/graphql/queries/AdminQueries";
import { request as graphqlRequest } from "graphql-request"
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
dayjs.extend(utc);
export async function loader({ request }) {
await authenticate.admin(request);
const response = await graphqlRequest(process.env.APP_GRAPHQL_URI, QUERY_PAGES);
const pageLayouts = response.GetPages;
return json({ pageLayouts });
}
export default function Pages() {
const { pageLayouts } = useLoaderData();
const navigate = useNavigate();
function getBadgeTone(status: String) {
switch (status) {
case 'Active':
return 'success';
case 'Expired':
return 'critical';
case 'Disabled':
return 'default';
case 'Draft':
return 'attention'
default:
return 'default';
}
}
const resourceName = {
singular: 'pageLayout',
plural: 'pageLayouts',
};
const {selectedResources, allResourcesSelected, handleSelectionChange} =
useIndexResourceState(pageLayouts);
const rowMarkup = pageLayouts.map(
(
{internalId, preview, name, keywords, isDated, category, createdAt, updatedAt, status},
index,
) => (
<IndexTable.Row
id={internalId}
key={internalId}
selected={selectedResources.includes(internalId)}
position={index}
>
<IndexTable.Cell>
<Text variant="bodyMd" fontWeight="bold" as="span">
{internalId}
</Text>
</IndexTable.Cell>
<IndexTable.Cell><Thumbnail source={preview} alt="thumbnail"/></IndexTable.Cell>
<IndexTable.Cell><Link to="url">{name}</Link></IndexTable.Cell>
<IndexTable.Cell>{keywords.join(', ')}</IndexTable.Cell>
<IndexTable.Cell>{isDated ? "Dated" : "Undated"}</IndexTable.Cell>
<IndexTable.Cell>{category.name}</IndexTable.Cell>
<IndexTable.Cell><Badge tone={getBadgeTone(status)}>{status}</Badge></IndexTable.Cell>
<IndexTable.Cell>{dayjs(createdAt).format('DD/MM/YY h:mmA')}</IndexTable.Cell>
<IndexTable.Cell>{dayjs(updatedAt).format('DD/MM/YY h:mmA')}</IndexTable.Cell>
</IndexTable.Row>
),
);
const promotedBulkActions = [
{
icon: DeleteIcon,
destructive: true,
content: 'Delete pages',
onAction: () => console.log('Todo: implement deleting customers'),
},
];
const EmptyLayoutState = () => (
<EmptyState
heading="Add page layouts for customisable planners."
action={{
content: "Add Layout",
onAction: () => {
console.log("Navigate to new page");
navigate('/app/pages/new')
}
}}
image="https://cdn.shopify.com/s/files/1/0262/4071/2726/files/emptystate-files.png"
>
</EmptyState>
);
return (
<Page
fullWidth={true}
title="Page Layouts"
subtitle="test subtitle"
primaryAction={{
content: 'Add Layout',
onAction: () => {
console.log("Navigate to new page");
navigate('/app/pages/new')
}
}
}
>
<Layout>
<Layout.Section>
<Card>
{pageLayouts.length === 0 ? (
<EmptyLayoutState/>
) : (
<IndexTable
resourceName={resourceName}
itemCount={pageLayouts.length}
selectedItemsCount={
allResourcesSelected ? 'All' : selectedResources.length
}
promotedBulkActions={promotedBulkActions}
onSelectionChange={handleSelectionChange}
headings={[
{title: 'ID'},
{title: 'Preview'},
{title: 'Page name'},
{title: 'Keywords'},
{title: 'Dated / Undated'},
{title: 'Category'},
{title: 'Status'},
{title: 'Date Added'},
{title: 'Date Modified'},
]}>
{rowMarkup}
</IndexTable>
)}
</Card>
</Layout.Section>
</Layout>
</Page>
);
}
</code>
<code>import { Card, Layout, Page, Text, IndexTable, Thumbnail, useIndexResourceState, Badge, EmptyState, } from "@shopify/polaris"; import { useLoaderData, useNavigate, Link } from "@remix-run/react"; import { json } from "@remix-run/node"; import { authenticate } from "~/shopify.server"; import { DeleteIcon } from "@shopify/polaris-icons"; import { QUERY_PAGES } from "~/graphql/queries/AdminQueries"; import { request as graphqlRequest } from "graphql-request" import dayjs from "dayjs"; import utc from "dayjs/plugin/utc"; dayjs.extend(utc); export async function loader({ request }) { await authenticate.admin(request); const response = await graphqlRequest(process.env.APP_GRAPHQL_URI, QUERY_PAGES); const pageLayouts = response.GetPages; return json({ pageLayouts }); } export default function Pages() { const { pageLayouts } = useLoaderData(); const navigate = useNavigate(); function getBadgeTone(status: String) { switch (status) { case 'Active': return 'success'; case 'Expired': return 'critical'; case 'Disabled': return 'default'; case 'Draft': return 'attention' default: return 'default'; } } const resourceName = { singular: 'pageLayout', plural: 'pageLayouts', }; const {selectedResources, allResourcesSelected, handleSelectionChange} = useIndexResourceState(pageLayouts); const rowMarkup = pageLayouts.map( ( {internalId, preview, name, keywords, isDated, category, createdAt, updatedAt, status}, index, ) => ( <IndexTable.Row id={internalId} key={internalId} selected={selectedResources.includes(internalId)} position={index} > <IndexTable.Cell> <Text variant="bodyMd" fontWeight="bold" as="span"> {internalId} </Text> </IndexTable.Cell> <IndexTable.Cell><Thumbnail source={preview} alt="thumbnail"/></IndexTable.Cell> <IndexTable.Cell><Link to="url">{name}</Link></IndexTable.Cell> <IndexTable.Cell>{keywords.join(', ')}</IndexTable.Cell> <IndexTable.Cell>{isDated ? "Dated" : "Undated"}</IndexTable.Cell> <IndexTable.Cell>{category.name}</IndexTable.Cell> <IndexTable.Cell><Badge tone={getBadgeTone(status)}>{status}</Badge></IndexTable.Cell> <IndexTable.Cell>{dayjs(createdAt).format('DD/MM/YY h:mmA')}</IndexTable.Cell> <IndexTable.Cell>{dayjs(updatedAt).format('DD/MM/YY h:mmA')}</IndexTable.Cell> </IndexTable.Row> ), ); const promotedBulkActions = [ { icon: DeleteIcon, destructive: true, content: 'Delete pages', onAction: () => console.log('Todo: implement deleting customers'), }, ]; const EmptyLayoutState = () => ( <EmptyState heading="Add page layouts for customisable planners." action={{ content: "Add Layout", onAction: () => { console.log("Navigate to new page"); navigate('/app/pages/new') } }} image="https://cdn.shopify.com/s/files/1/0262/4071/2726/files/emptystate-files.png" > </EmptyState> ); return ( <Page fullWidth={true} title="Page Layouts" subtitle="test subtitle" primaryAction={{ content: 'Add Layout', onAction: () => { console.log("Navigate to new page"); navigate('/app/pages/new') } } } > <Layout> <Layout.Section> <Card> {pageLayouts.length === 0 ? ( <EmptyLayoutState/> ) : ( <IndexTable resourceName={resourceName} itemCount={pageLayouts.length} selectedItemsCount={ allResourcesSelected ? 'All' : selectedResources.length } promotedBulkActions={promotedBulkActions} onSelectionChange={handleSelectionChange} headings={[ {title: 'ID'}, {title: 'Preview'}, {title: 'Page name'}, {title: 'Keywords'}, {title: 'Dated / Undated'}, {title: 'Category'}, {title: 'Status'}, {title: 'Date Added'}, {title: 'Date Modified'}, ]}> {rowMarkup} </IndexTable> )} </Card> </Layout.Section> </Layout> </Page> ); } </code>
import {
  Card,
  Layout,
  Page,
  Text,
  IndexTable,
  Thumbnail,
  useIndexResourceState,
  Badge,
  EmptyState,
} from "@shopify/polaris";
import { useLoaderData, useNavigate, Link } from "@remix-run/react";
import { json } from "@remix-run/node";
import { authenticate } from "~/shopify.server";
import { DeleteIcon } from "@shopify/polaris-icons";
import { QUERY_PAGES } from "~/graphql/queries/AdminQueries";
import { request as graphqlRequest } from "graphql-request"
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
dayjs.extend(utc);

export async function loader({ request }) {
  await authenticate.admin(request);
  const response = await graphqlRequest(process.env.APP_GRAPHQL_URI, QUERY_PAGES);
  const pageLayouts = response.GetPages;

  return json({ pageLayouts });
}

export default function Pages() {

  const { pageLayouts } = useLoaderData();
  const navigate = useNavigate();

  function getBadgeTone(status: String) {
    switch (status) {
      case 'Active':
        return 'success';
      case 'Expired':
        return 'critical';
      case 'Disabled':
        return 'default';
      case 'Draft':
        return 'attention'
      default:
        return 'default';
    }
  }

  const resourceName = {
    singular: 'pageLayout',
    plural: 'pageLayouts',
  };

  const {selectedResources, allResourcesSelected, handleSelectionChange} =
    useIndexResourceState(pageLayouts);

  const rowMarkup = pageLayouts.map(
    (
      {internalId, preview, name, keywords, isDated, category, createdAt, updatedAt, status},
      index,
    ) => (
      <IndexTable.Row
        id={internalId}
        key={internalId}
        selected={selectedResources.includes(internalId)}
        position={index}
      >
        <IndexTable.Cell>
          <Text variant="bodyMd" fontWeight="bold" as="span">
            {internalId}
          </Text>
        </IndexTable.Cell>
        <IndexTable.Cell><Thumbnail source={preview} alt="thumbnail"/></IndexTable.Cell>
        <IndexTable.Cell><Link to="url">{name}</Link></IndexTable.Cell>
        <IndexTable.Cell>{keywords.join(', ')}</IndexTable.Cell>
        <IndexTable.Cell>{isDated ? "Dated" : "Undated"}</IndexTable.Cell>
        <IndexTable.Cell>{category.name}</IndexTable.Cell>
        <IndexTable.Cell><Badge tone={getBadgeTone(status)}>{status}</Badge></IndexTable.Cell>
        <IndexTable.Cell>{dayjs(createdAt).format('DD/MM/YY h:mmA')}</IndexTable.Cell>
        <IndexTable.Cell>{dayjs(updatedAt).format('DD/MM/YY h:mmA')}</IndexTable.Cell>
      </IndexTable.Row>
    ),
  );

  const promotedBulkActions = [
    {
      icon: DeleteIcon,
      destructive: true,
      content: 'Delete pages',
      onAction: () => console.log('Todo: implement deleting customers'),
    },
  ];


  const EmptyLayoutState = () => (
    <EmptyState
      heading="Add page layouts for customisable planners."
      action={{
        content: "Add Layout",
        onAction: () => {
          console.log("Navigate to new page");
          navigate('/app/pages/new')
        }
      }}
      image="https://cdn.shopify.com/s/files/1/0262/4071/2726/files/emptystate-files.png"
    >
    </EmptyState>
  );

  return (
    <Page
    fullWidth={true}
    title="Page Layouts"
    subtitle="test subtitle"
    primaryAction={{
      content: 'Add Layout',
      onAction: () => {
        console.log("Navigate to new page");
        navigate('/app/pages/new')
      }
      }
    }
    >
      <Layout>
        <Layout.Section>
          <Card>
            {pageLayouts.length === 0 ? (
              <EmptyLayoutState/>
            ) : (
              <IndexTable
              resourceName={resourceName}
              itemCount={pageLayouts.length}
              selectedItemsCount={
                allResourcesSelected ? 'All' : selectedResources.length
              }
              promotedBulkActions={promotedBulkActions}
              onSelectionChange={handleSelectionChange}
              headings={[
                {title: 'ID'},
                {title: 'Preview'},
                {title: 'Page name'},
                {title: 'Keywords'},
                {title: 'Dated / Undated'},
                {title: 'Category'},
                {title: 'Status'},
                {title: 'Date Added'},
                {title: 'Date Modified'},
              ]}>
              {rowMarkup}
            </IndexTable>
            )}
          </Card>
        </Layout.Section>
      </Layout>
    </Page>
  );
}

New contributor

Katy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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