-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dev hint about css object being stringified by accident #1219
Conversation
Preview Docs Built with commit 2c61e9d |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a big fan of this, while it would help some people, I'm worried that toString will be called for random reasons(iirc react-hot-loader might have done this) and people will open issues with "why is there some random error, i'm not doing what it says i'm doing". This used to be problem with the component selector error, it used to throw on toString but toString was getting called for random reasons and it was expected that it didn't have side effects and didn't throw so the error was moved to serialization.
Hm, maybe it was called on components because they were components that react hot loader had tried to patch somehow? Not sure why regular object would get stringified by accident 🤔 |
I had an idea, what if we returned this message from toString so when people inspect the dom, they'll see the message but toString doesn't have any side effects that could cause confusion if somehow stringified for another reason? |
Codecov Report
|
Not sure if I follow - how people inspecting the DOM would see our message? |
Something like this, https://codesandbox.io/s/73kjljkzv6 |
Ok, this is clever XD So you would basically like to move the error message from console.error to return? |
Yes. |
2c61e9d
to
308f5f4
Compare
🦋 Changeset is good to goLatest commit: c20e955 We got this. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
308f5f4
to
db0a9f1
Compare
@mitchellhamilton I've updated this long-forgotten PR. Should be ready to merge now. |
packages/core/__tests__/warnings.js
Outdated
|
||
test('`css` opaque object passed in as `className` prop', () => { | ||
// react-test-renderer doesn't stringify `className` (unlike react-dom) | ||
// we need to pass dummy `css` prop so `jsx` tries to handle it (and stringify the `className`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use react testing library so we're using react-dom here and encounter what will happen in the browser?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
used testing library
db0a9f1
to
9311b08
Compare
I've adjusted the PR according to your suggestion 👍 I must say though that I'm not entirely sold on that |
9311b08
to
58a53a8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!!
@mitchellhamilton what about my reservations about just using toString trick without console.error? Should we switch back to console.error or do you think this is fine as is? |
I think it's fine as it is. Based on the "why is there [object Object] on my element" issues, I think it'll address the problem because the first thing people will probably do if a style isn't being applied is inspect the element where they'll see this text. Also, I want to avoid toString functions that log/have visible side effects in general because we've been burned by it in the past when the error about not using the babel plugin when using component selectors showed up when people weren't even interpolating it because it was in the toString(it's since been moved to serialize) |
It might be helpful to add some extra note in the error message that you can use |
@mycarrysun could you elaborate more on this? I'm not exactly sure what you are proposing |
@Andarist the goal of my feature was to add styles to all children but also allow the children to declare their own styles if necessary. Here is the component: import { css as createEmotionClassName } from 'emotion';
export function FormAccessibleWrapper({
children,
}) {
return React.Children.map(children, (child) => {
const { props: { css: childCss } = {} } = child;
return React.cloneElement(child, {
className: createEmotionClassName([
styles.base,
includeHoverStyles && styles.hover,
includeFocusStyles && styles.focus,
includeActiveStyles && styles.active,
childCss,
]),
});
});
} Originally I had tried to do: export function FormAccessibleWrapper({
children,
}) {
return React.Children.map(children, (child) => {
const { props: { css: childCss } = {} } = child;
return React.cloneElement(child, {
css: [
styles.base,
includeHoverStyles && styles.hover,
includeFocusStyles && styles.focus,
includeActiveStyles && styles.active,
childCss,
],
});
});
} It would be great to know that I needed to use the Here is an error message that would be more helpful:
Might be a better way to word the "passing it into css()" but hopefully you get my drift |
No description provided.