question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useToken 'breakpoints' not returning values

See original GitHub issue

Description

When I use ‘breakpoints’ in useToken, I expect it to return a width value

Link to Reproduction

https://codesandbox.io/s/flamboyant-poincare-19xm1

Steps to reproduce

const [sm, lg] = useToken("breakpoints", ["sm", "lg"]);
const [red500] = useToken("colors", ["red.500"]);

console.log({ sm, lg, red500 });

Chakra UI Version

1.6.12

Browser

Chrome Version 95.0.4638.69 (Official Build) (x86_64)

Operating System

  • macOS
  • Windows
  • Linux

Additional Information

Looks like this broke in this release #4867 (system@1.7.5), because its still working in #4799 (system@1.7.4)

Looking a little deeper, it looks like its caused by this change because breakpoints is not a part of __cssMap

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
IHIutchcommented, Nov 10, 2021

I understand what is causing the issue, I identified that myself in my initial comment.

The useToken hook as defined is

The useToken hook retrieves whatever is in the theme at the given path(s).

This is no longer true and causes unexpected behavior. There is no mention of a tokens relationship with CSS variables.

It’s also an undocumented breaking change for anyone using breakpoints with useToken, which doesn’t throw an error.

1reaction
primos63commented, Nov 10, 2021

Your sandbox is not going to work as you have the useToken calls before the ChakraProvider is invoked. The theme is not available at the time you’re making the call so it always returns the default value which in this case the values you requested. This is how the app should be written

import React from "react";
import ReactDOM from "react-dom";
import { Box, ChakraProvider, useToken, Text } from "@chakra-ui/react";
import { get } from "@chakra-ui/utils"
import { useTheme } from "@emotion/react";

function App() {
  const theme = useTheme()
  const [sm, lg] = ['sm', 'lg'].map(bkpt => get(theme, `breakpoints.${bkpt}`, bkpt));
  const [red500] = useToken("colors", ["red.500"]);

  return (
    <Box padding={4}>
      <Text>{sm}</Text>
      <Text>{lg}</Text>
      <Text>{red500}</Text>
    </Box>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(
  <ChakraProvider>
    <App />
  </ChakraProvider>,
  rootElement
);

It will produce the output

30em
62em
#E53E3E

The breakPoints are not in the __cssMap. I think this is because they are used by media queries so they don’t need to be css variables.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useBreakpointValue - Chakra UI
useBreakpointValue is a custom hook which returns the value for the current breakpoint from the provided responsive values object.
Read more >
Breakpoints not getting hit while debugging in VS10
When I put a breakpoint in the setter and I change the value of Value from the control the breakpoint is not getting...
Read more >
Breakpoints - NativeBase
useBreakpointValue is a custom hook which returns the value for the current breakpoint from the provided responsive values object.
Read more >
Troubleshoot Breakpoints in the Visual Studio Debugger
"No Symbols have been loaded for this document". Go to the Modules window (Debug > Windows > Modules) and check whether your module...
Read more >
Debugger steps not working in C: hovering over values do not ...
Debugger steps not working in C: hovering over values do not return values, ... ability to retrieve variable values (or execute watches) in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found