score:0
Accepted answer
it's not working because you have your styled component in app function. every rerender it's being declared again, so transition can't happen. simply move styled components outside function to fix it. i'm passing props here to styled component to change color.
import react from "react";
import reactdom from "react-dom";
import styled, { css } from "styled-components";
import "./styles.css";
const styledsquare = styled.div`
color: white;
margin: 10px;
width: 100px;
height: 100px;
background-color: ${props => props.red ? "red": "blue"};
transition: background-color 2s linear;
`;
function app() {
const [red, setred] = react.usestate(false);
function handleclick() {
setred(!red);
}
return (
<div classname="app">
<styledsquare red={red}>styled component</styledsquare>
<button onclick={handleclick}>click</button>
</div>
);
}
const rootelement = document.getelementbyid("root");
reactdom.render(<app />, rootelement);
Source: stackoverflow.com
Related Query
- How to make a styled component work with css transition?
- How to get Emotion styled component in a React project to work with TypeScript?
- How to make dark mode of Tailwind CSS work with Next.js and styled-jsx?
- how to make css stylesheet to work in reactjs if components are in seperate component folder
- How to make Flexbox work with children when using horizontal MUI Collapse Component and TransitionGroup transitions?
- How to make my custom tab component work with passing index to children and hiding panes?
- how to make component size variation with reactjs and styled components
- How can I make the card component work with react-split-pane component?
- how to make react-hook-form work with multiple forms in one page?
- How to make react router work with static assets, html5 mode, history API and nested routes?
- How can I make React Portal work with React Hook?
- How to make Puppeteer work with a ReactJS application on the client-side
- How should customRequest be set in the Ant Design Upload component to work with an XMLHttpRequest?
- How can I make intro js work with React?
- How to make useRouteMatch work with types from react-router
- How to add properties to styled component with Typescript?
- How to design a ReactJS component that listens to a WebSocket and interacts with CSS animation
- How to work with styled components in my react app?
- How to conditionally apply styles in a react component with CSS Modules?
- how to change image src using props with styled component and react
- How to bundle CSS with a component as an npm package?
- Typescript with Styled component conditional css for custom props
- How to make svg react component imports from external module working with create-react-app?
- How to make an Accordion component with Reactstrap?
- How to type a styled component without losing any prop with Typescript?
- How to render a React component styled with Emotion in an iframe in Gatsby site?
- How to get access to CSS values from a styled component (React)?
- How to override React Component styles with styled components?
- How to modify the wrapper css rule of a Tabs component to make the text aligned to the left in a vertical tab in Material-UI?
- How to make a React multi-step form work with React Router?
More Query from same tag
- Problems updating to React 15.4.0
- What's causing this async function to work only after a page refresh?
- Attaching absolute path to css/js files while building the react app
- Component stopped to re-render after using privateroute
- React renders based on state change but not on prop change?
- Sending data to and getting data from node.js?
- import & export may only appear at the top level - CRA React App with Typescript
- Why the input doesnt work when generated by React
- How do I Link Components correctly using React Router?
- Why is Cloud Firestore not allowing access with Firebase Auth validation?
- React Component does not re-render on context change with useContext
- Cannot Display the Data From the object from an API
- How to make a border-radius for Skeleton by react-loading-skeleton?
- useEffect missing dependencies 'props'
- Material UI datetime-local input open the calendar dropdown
- When I output the JavaScript map function to JSX, the value comes out strangely
- Find a component using id in React testing
- How to refactor a React hook so that I can call it inside a callback?
- Adding component to page when more specific route is used
- How do you use P5 loadImage() with React props from FileReader?
- Babel configuration with decorators & class properties not working
- Fetch GET request with Reactjs - how do i implement a reusable component within my request to render my data?
- How to setState in React when async mapping through array and API call
- Why isn't there a "class" prop for React?
- Why don't chaining dispatch Redux-Thunk
- React router not loading script from public/index.html
- Flask + React + Nginx: pages showing 404 not found for all routes with port number 80
- Google-Map-React how to get markers inside bounds?
- How to format date in react-calendar?
- React - Generate input field via dropdown