In webpack, css-loader I use localIdentName hash:base64:10 to "hide" real class names.
I have also some global components to put inside DOM structure page.
But, sometimes I need change style component form parent. When I use explicit class names (local), styles is working, but after render with hash, "duplicates" class name has diffrent hash and not overrite styles.
I mean about .window .paper - I want to overite .paper form a parent, but I haven't idea how can I get rendered hash name from file.
structure:
|-main.js
|-style.less
|-paper
|-index.js
|-style.less
main.js
import React from 'react';
import s from "./style.less";
import Paper from './paper/index.js';
export class Main extends React.Component {
render() {
return (
<div className={s.window}>
<Paper>example text</Paper>
</div>
)
}
}
style.less
#import 'paper/style.less';
.window {
background-color: rgba(0, 0, 0, 0.6);
backdrop-filter: blur(10px);
width: 100%;
height: 100%;
.paper { //<-----
background-color: yellow;
}
}
paper/index.js:
import React from 'react';
import s from "./paper/style.less";
export class Paper extends React.Component {
render() {
const { children } = this.props;
return (
<div className={s.paper}>
<div className={s.content} ref={...}>{children}</div>
</div>
)
}
}
paper/style.less:
.paper { //<-----
display: block;
background-color: white;
border: 1px solid black;
.content {
margin: 20px;
}
}
Related
SandBox: https://codesandbox.io/s/infallible-nash-x91zz?file=/src/App.js
In the sandbox I have an emotion style defined. It has two classes it in — open and closed.
I am using a state to toggle the classes and the classes are toggling correctly according to the inspector.
Problem: styles not updating on state change.
Expected behavior: background color on div will change when class changes between open and closed
Actual Behavior: The classes are being updated but the stiles are not.
Code:
import React, { useState } from "react";
import "./styles.css";
import styled from "#emotion/styled";
const MenuContainer = styled.div`
.open {
background-color: blue;
width: 600px;
height: 600px;
}
.closed {
background-color: red;
width: 600px;
height: 600px;
}
`;
export default function App() {
const [openState, setOpenState] = useState(false);
return (
<MenuContainer className={openState ? "closed" : "open"}>
<button value="click" onClick={() => setOpenState(!openState)}>
Click Me
</button>
</MenuContainer>
);
}
You should do this:
const MenuContainer = styled.div`
width: 600px;
height: 600px;
&.open {
background-color: blue;
}
&.closed {
background-color: red;
}
`;
Like the title says, this works with styled components sitting within the same js file (provided they are procedural-ordered above). But with imported child components I can't get it to work.
import React from "react";
import styled from "styled-components";
// Components
import Bars from "../path/Bars.js";
import BarsHover from "../path/BarsHover.js";
// Variables
import { colors } from "../../path/index.js";
//============================================ styles =============================================
const DivBars = styled(Bars)``;
const DivBarsHover = styled(BarsHover)``;
const DivWrapper = styled.div`
display: flex;
width: 20rem;
margin-bottom: 3rem;
&:hover {
cursor: pointer;
}
&:hover ${DivBars} {
display: none;
}
&:hover ${DivBarsHover} {
display: block;
}
`;
//=========================================== component ===========================================
const ParentComponent = props => {
return (
<DivContainer>
<DivBars fullBarWidth={"100%"} fractionBarWidth={"70%"} barColor={colors.niagara} />
<DivBarsHover fullBarWidth={"100%"} fractionBarWidth={"70%"} barColor={colors.gallery2} />
</DivContainer>
);
};
export default ParentComponent;
I think this caveat is the cause:
...wrapping A in a styled() factory makes it eligible for
interpolation -- just make sure the wrapped component passes along
className.
class A extends React.Component {
render() {
return <div className={this.props.className} />
}
}
const StyledA = styled(A)``
const B = styled.div`
${StyledA} {
}
`
NOTE: Ensure the className prop is propagated all the way to the component being referenced in the case that it isn't a direct descendent.
I am working on Reactjs project, I created one Component and named it as Students, In that in Students.js one error is showing that is Typo in component lifecycle method declaration react/no-typos I don't know why
it is showing, So please help me debug this issue.
This is App.js
import React from 'react';
import './App.css';
import Students from './Students/Students'
function App() {
return (
<div className="App">
<Students></Students>
</div>
);
}
export default App;
This is App.css
.App {
text-align: center;
}
.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 40vmin;
pointer-events: none;
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
#keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
This is Student.js
import React, { Component } from 'react';
import './Students.css';
class Students extends Component {
Render() {
return(
<div className='container'>
<div className='row'>
<h1>Hello</h1>
</div>
</div>
)
}
}
export default Students
This is Student.css
There is no css in Student.css
Component lifecycle methods declaration in react should be camelCased. Otherwise It would be typo. Please make your Rendor() method's letter 'R' in lowercase (render()) to correct this issue.
Please forgive my confusion and newbiness. I'm trying to export a styled Button. Totally dazed and confused. Please help. I don't really want to export 2 buttons, as shown, but a single Button with the styles from the props, and the given styles as the default, I think :(
import React, {Component} from 'react'
import ReactDOM from 'react-dom'
import styled, { css } from 'styled-components'
export default class Button extends React.Component {
btn: Btn = (props) => {
styled.button`
border-radius: 3px;
padding: 0.25em 1em;
margin: 0 1em;
background: transparent;
color: palevioletred;
border: 2px solid palevioletred;
${props => props.primary && css`
background: palevioletred;
color: white;
`}
`
}
render(
<Btn>Normal Button</Btn>
<Btn primary>Primary Button</Btn>
)
}
And my App element in case it's relevant
import React, { Component } from 'react'
import 'containers/App.css'
import Button from 'components/Button'
export default class App extends Component {
render() {
return (
<div>
<p>
<Button primary="primary" label="Button Help" />
</p>
</div>
)
}
}
Define your Styled Button as follows
import React from 'react';
import styled, {css} from 'react-emotion';
const StyledButton = styled('button')`
border-radius: 3px;
padding: 0.25em 1em;
margin: 0 1em;
background: transparent;
color: palevioletred;
border: 2px solid palevioletred;
`;
const primary = css`
background: black;
color: white;
`;
export default class Button extends React.Component {
render() {
return (
<div>
<StyledButton className={this.props.primary && `${primary}`}>
{this.props.label}
</StyledButton>
</div>
);
}
}
and in app element use buttton as follows
import React, { Component } from 'react'
import 'containers/App.css'
import Button from 'components/Button'
export default class App extends Component {
render() {
return (
<div>
<p>
<Button label="Button Help" /> // for normal Styled Button
<Button primary label="Button Primary" /> // for Primary Styled Button
</p>
</div>
)
}
}
I am trying to import and render an object:property of type React 'element' from external file and import it using module.exports into another file containing a Component and render it inside the Component. The component has previously been called 3 times, to create 3 columns and fill them with text.
This works when importing 'text' however I cannot get it to work importing a React 'element'.
What do I need to do to render the imported React 'element'? I am also using css-modules. Below is the code. Thanks:-
(File: column.css)
.default {
box-sizing: border-box;
border: 1px solid black;
font-size: 20px;
width: 32%;
height: auto;
}
.red {
composes: default;
float: left;
margin-right: 2%;
background-color: red;
}
.green {
composes: default;
float: left;
background-color: green;
}
.blue {
composes: default;
float: right;
margin-left: 2%;
background-color: white;
}
(File: Home.js)
import React from 'react'
import Column from '../components/Column'
import styles from './home.css'
export default class Home extends React.Component {
render() {
return (
<div className={styles.container}>
<h1>Home</h1>
<Column style = {'red'} content={'firstColumn'}/>
<Column style = {'green'} content={'secondColumn'}/>
<Column style = {'blue'} content={'thirdColumn'}/>
</div>
)
}
}
(File: Column.js)
import React from 'react'
import style from './column.css'
const contents = require( '../components/content')
export default class Column extends React.Component {
render() {
return (
<div className={style[this.props.style]}>
{contents[this.props.content]}
</div>
)
}
}
(File: content.js)
import React from "react"
module.exports = {
firstColumn: text,
secondColumn: "This text is rendered",
thirdColumn: "This text is rendered",
}
const text = <p>This element text is NOT rendered</p>;
Hm ... shouldn't your content.js looks like:
const text = <p>This element text is NOT rendered</p>;
module.exports = {
firstColumn: text,
secondColumn: "This text is rendered",
thirdColumn: "This text is rendered",
}
the text variable is not hoisted so you're exporting undefined as firstColumn.