I created a new react app using create-react-app
and then installed styled-component
using npm
. But when I use it in component, I am getting following error.
Failed to compile. ./src/Components/Lightbox/styledLightbox.js
Module not found:
Can't resolve 'styled-component' in '/Users/ishan/Documents/react-app/src/Components/Lightbox'
Following is the component:
import React, { Component } from 'react';
import { LightboxWrapper } from './styledLightbox';
class Lightbox extends Component {
renderEntry() {
if (this.props.lightbox.type === "photo") {
return <img alt="name" src={this.props.lightbox.entry} />;
}
if (this.props.lightbox.type === "video") {
return <video src={this.props.lightbox.entry} onLoadedMetadata={(e) => {console.log("Video duration is: "+e.currentTarget.duration) } } autoPlay muted onEnded={() => console.log("Video ended")} width='100%' height='100%' ></video>
}
if(this.props.lightbox.type === "text"){
return <div> <p className="lightbox text"> {this.props.lightbox.entry} </p> </div>
}
}
render() {
let classList = this.props.lightbox.status === true ? "lightbox-wrapper active" : "lightbox-wrapper";
return (
<LightboxWrapper className={classList}>
{/* {this.renderEntry()} */}
</LightboxWrapper>
);
}
}
export default Lightbox;
Following is the styled-component created to use it in other component:
import styled from "styled-component";
export const LightboxWrapper = styled.div`
display: none;
position: fixed;
z-index: 100;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background: radial-gradient(
center,
ellipse farthest-corner,
rgba(255,255,255,0) 0%,
rgba(255,255,255,0) 100%
);
.active {
display: block;
background: radial-gradient(
center,
ellipse farthest-corner,
rgba(255,255,255,0.5) 0%,
rgba(255,255,255,0.1) 100%
);
}
.active img, .active video {
max-width: 70%;
height: auto !important;
margin: 0 auto;
opacity: 1;
/* box-shadow: 0px 2px 7px rgba(0,0,0,0.2); */
transition: opacity 0.5s linear;
animation: fadeInScale 1.2s ease-in-out;
max-height: 70%;
width: auto !important;
position: relative;
top: 11%;
}
`;
Make sure that your file's letter-casing correctly matches with its respective static imports. Also make sure that your letter-casing in your filenames and imports are identical between your repository and local machine. If you are using git , make sure git config core. ignorecase false is set in your environment.
Advantages of using Styled-components Eliminates class name bugs: styled-components provide unique class names for your styles, thus eliminating the problems with class names duplications, misspellings, and overlaps.
styled-jsx v3 comes with a webpack loader that lets you write styles in regular css files and consume them in React. To consume the styles in your component you can import them from your CSS file and render them using a <style jsx> tag. Remember to add the global prop if you want your styles to be global.
You are missing the "s" at the end of "styled-components"
$ npm i styled-components
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With