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.

React hooks do not overwrite styles [TypeScript]

See original GitHub issue

Environment

System:

  • OS: macOS 10.14.4
  • CPU: (4) x64 Intel® Core™ i5-6287U CPU @ 3.10GHz
  • Memory: 166.97 MB / 16.00 GB
  • Shell: 5.3 - /bin/zsh

Binaries:

  • Node: 10.15.2 - /usr/local/bin/node
  • Yarn: 1.15.2 - /usr/local/bin/yarn
  • npm: 6.9.0 - /usr/local/bin/npm
  • Watchman: 4.9.0 - /usr/local/bin/watchman

npmPackages:

  • babel-plugin-styled-components: ^1.10.0 => 1.10.0
  • styled-components: ^4.1.3 => 4.1.3

Reproduction

I import a file which is a React Hook and want to override the color. But there is no effect on that 😢

// Modules
import React, { Fragment } from "react";
import styled from "styled-components";

// Hook
const MyHookTitle = () => {
  const InternalTitle = styled.h1`
    color: purple;
  `;
  return <InternalTitle>MyHookTitle</InternalTitle>;
};

// Styles
const StyledHook = styled(MyHookTitle)`
  color: red;
`;

export const App = () => {
  return (
    <Fragment>
      <MyHookTitle />
      <StyledHook />
    </Fragment>
  );
};

Expected Behavior

Color of title is red. image

Actual Behavior

Color is purple image

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
epilandecommented, Apr 10, 2019

@nachbarshund InternalTitle is still in the render lifecycle. As mentioned in https://github.com/styled-components/styled-components/issues/2477#issuecomment-479510128, we shouldn’t be doing that. InternalTitle needs to be moved outside of MyHookTitle and the className needs to be passed down in order for StyledHook’s styles to apply.

// Modules
import React, { Fragment } from "react";
import styled from "styled-components";

const InternalTitle = styled.h1`
  color: purple;
`;

// Hook
const MyHookTitle = ({ className }) => {
  return <InternalTitle className={className}>MyHookTitle</InternalTitle>;
};

// Styles
const StyledHook = styled(MyHookTitle)`
  color: red;
`;

const App = () => (
  <Fragment>
    <MyHookTitle />
    <StyledHook />
  </Fragment>
);

Here’s a codesandbox of the above code https://codesandbox.io/s/9ol8molq4o

0reactions
nachbarshundcommented, Apr 11, 2019

@epilande Oh man big thanks. No I’ll get the point here. Thanks for helping 🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript and React: Hooks - fettblog.eu
A function that is called without any parameters. This is the side-effect you want to call. · An array of values of type...
Read more >
Rules of Hooks - React
They let you use state and other React features without writing a class. Hooks are JavaScript functions, but you need to follow two...
Read more >
React Hooks cheat sheet: Best practices with examples
React useState and setState don't make changes directly to the state object; they create queues to optimize performance, which is why the ...
Read more >
React Hooks - Modified state not reflecting immediately
Much like setState, the state update behaviour using hooks will also require a re-render and update and hence the change will not be ......
Read more >
FAQs - styled-components
If you're using React Native, you'll need at least v0.59 (the first version to support hooks.) That's it.. styled-components v5 does not introduce...
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