Here is my code for getting alert on page load.
componentDidMount() {
alert("Hi");
}
public render(): React.ReactElement<IReactCrudProps> {
return (
<select id="myDDl">
</select>
);
}
componentDidMount() {
alert("Hi");
}
public render(): React.ReactElement<IReactCrudProps> {
return (
<select id="myDDl">
</select>
);
}
<select value={this.state.selectValue} onChange={this.handleChange}>
<option value="Orange">Orange</option>
<option value="Radish">Radish</option>
<option value="Cherry">Cherry</option>
</select>
);
-
with camelCase text. You can style them in two ways using JavaScript objects as shown in the example.import React from "react";
const spanStyles = {
color: "#fff",
borderColor: "#00f"
};
const Button = props => (
<button style={{
color: "#fff",
borderColor: "#00f"
}}>
<span style={spanStyles}>Button Name</span>
</button>
);
a:link {
color: gray;
}
a:visited {
color: green;
}
a:hover {
color: rebeccapurple;
}
a:active {
color: teal;
}
jss
, jss-preset-default
, jss-cli
import React from "react";
import injectSheet from "react-jss";
// Create your Styles. Remember, since React-JSS uses the default preset,
// most plugins are available without further configuration needed.
const styles = {
myButton: {
color: "green",
margin: {
// jss-expand gives more readable syntax
top: 5, // jss-default-unit makes this 5px
right: 0,
bottom: 0,
left: "1rem"
},
"& span": {
// jss-nested applies this to a child span
fontWeight: "bold" // jss-camel-case turns this into 'font-weight'
}
},
myLabel: {
fontStyle: "italic"
}
};
const Button = ({ classes, children }) => (
<button className={classes.myButton}>
<span className={classes.myLabel}>{children}</span>
</button>
);
// Finally, inject the stylesheet into the component.
const StyledButton = injectSheet(styles)(Button);
styled-components
import React from "react";
import styled, { css } from "styled-components";
const Button = styled.button`
cursor: pointer;
background: transparent;
font-size: 16px;
border-radius: 3px;
color: palevioletred;
margin: 0 1em;
padding: 0.25em 1em;
transition: 0.5s all ease-out;
${props =>
props.primary &&
css`
background-color: white;
color: green;
border-color: green;
`};
`;
export default Button;
css-loader
import React from "react";
import style from "./panel.css";
const Panel = () => (
<div className={style.panelDefault}>
<div className={style.panelBody}>A Basic Panel</div>
</div>
);
export default Panel;
.panelDefault {
border-color: #ddd;
}
.panelBody {
padding: 15px;
}
node-sass
$font-stack: 'Open Sans', sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}
less
, less-loader
@
sign in Less and with a $
in Sass.@pale-green-color: #4D926F;
#header {
color: @pale-green-color;
}
h2 {
color: @pale-green-color;
}
@stylable/core
, @stylable/runtime
, @stylable/optimizer
, @stylable/module-utils
, @stylable/custom-valu
@namespace "Page";
:import {
-st-from: './video-player.st.css';
-st-default: VideoPlayer;
}
.mainVideo {
-st-extends: VideoPlayer; /* define mainVideo as VideoPlayer */
}
.mainVideo::playButton { /* override mainVideo playButton */
background: green;
color: purple;
}
/* CSS output*/
.Page__mainVideo.VideoPlayer__root {}
.Page__mainVideo.VideoPlayer__root .VideoPlayer__playButton {
background: green;
color: purple;
}
.scss
files compile and write to .css
files right in our folder structure. You may be concerned about using Sass in React. Isn’t it a smarter way to write styles with CSS-in-JS libraries like styled-components or aphrodite? I believe that adding Sass support to Create React App will be a big help to beginners of React. How do I use Sass in React is one of the questions I always hear from people getting into React. With the React 16.6 additions like React.memo()
and the React 16.7 functional additions like hooks, starting with React will be easier than ever!npm install -g create-react-app
globally or using npx create-react-app
to install and invoke it immediately so your installed package won’t be anywhere in your globals. Check out more about npx.node-sass
dependencynpm install node-sass
to install node-sass
to help in compiling your scss
to css
.index.css
file to index.scss
file and trying out some cool Sass / SCSS features.$blue: #004BB4;
$ubuntu-font: 'Ubuntu', 'Arial', 'Helvetica', sans-serif;
$nunito-font: 'Nunito', 'Arial', 'Helvetica', sans-serif;
h1 {
font: $ubuntu-font;
color: $blue;
}
a {
font: $nunito-font;
background-color: $blue;
padding: 6px;
}
<ul class="navbar">
<li><a href="">Item <span>1</span></a></li>
<li><a href="">Item <span>2</span></a></li>
<li><a href="">Item <span>3</span></a></li>
<li><a href="">Item <span>4</span></a></li>
<li><a href="">Item <span>5</span></a></li>
</ul>
.navbar {
font: $ubuntu-font;
color: $blue;
li {
margin-left: 1rem;
a {
padding: 5px;
font-size: 1.5rem;
span {
font-weight: 600;
}
}
}
}
if()
, for()
or while()
, data types, interpolation, etc. Becoming a Sass guru may take a bit of time — all you need to do is look into the Bootstrap Sass files to see how Sass could turn into a complex thing. But learning the basics and setting it up for your project won’t take you long.