React is a popular library for creating web apps and mobile apps.
In this article, we’ll look at some tips for writing better React apps.
Conditionally Displaying Items in a React Component
We can conditionally display items in a React component in various ways.
One way is to use a ternary expression.
For instance, we can write:
render() {
return (
this.props.showMe ? <button type="submit">show me</button> : null
);
}
The first item is shown if showMe
is true
and null
is rendered otherwise.
We can also use the &&
operator.
For instance, we can write;
`{`showMe `&&` <button type="submit">show me</button>`}`
Is showMe
is true
then the button is rendered since the expression will be evaluated.
CSS Pseudo-elements in React
We can add prefixed styled by adding them as properties of the style
object.
For instance, we can write:
render() {
return (
<div>
<span>Something</span>
<div style={{ WebkitFilter: 'blur(10px) saturate(2)' }} />
</div>
);
},
We have the WebkitFilter
property which is equivalent to CSS’s -webkite-filter
property.
To make our lives raise,r we can use the style-it package to convert any CSS property to JavaScript style properties.
To install it, we run:
npm install style-it --save
Then we can use it by writing:
import React from "react";
import Style from "style-it";
class Intro extends React.Component {
render() {
return Style.it(
`
.tooltip {
display:inline-block;
position:relative;
border-bottom:1px dotted #666;
text-align:left;
}
.tooltip .right {
min-width:200px;
top:50%;
left:100%;
margin-left:20px;
transform:translate(0, -50%);
padding:10px 20px;
color:#444444;
background-color:#EEEEEE;
font-weight:normal;
font-size:13px;
border-radius:8px;
position:absolute;
z-index:99999999;
box-sizing:border-box;
box-shadow:0 1px 8px rgba(0,0,0,0.5);
display:none;
}
.tooltip:hover .right {
display:block;
}
.tooltip .right i {
position:absolute;
top:50%;
right:100%;
margin-top:-12px;
width:12px;
height:24px;
overflow:hidden;
}
.tooltip .right i::after {
content:'';
position:absolute;
width:12px;
height:12px;
left:0;
top:50%;
transform:translate(50%,-50%) rotate(-45deg);
background-color:#EEEEEE;
box-shadow:0 1px 8px rgba(0,0,0,0.5);
}
`,
<div id="tooltip" />
);
}
}
We use the Style
component with the CSS embedded in the string.
Then anything that comes after the string will be styled with the given styles.
Also, we can use the styled-components library to do the same thing.
We can install it by running:
npm i styled-components
Then we can write:
import React from 'react';
import styled from 'styled-components';
const Tooltip = styled.div`
.tooltip {
display:inline-block;
position:relative;
border-bottom:1px dotted #666;
text-align:left;
}
.tooltip .right {
min-width:200px;
top:50%;
left:100%;
margin-left:20px;
transform:translate(0, -50%);
padding:10px 20px;
color:#444444;
background-color:#EEEEEE;
font-weight:normal;
font-size:13px;
border-radius:8px;
position:absolute;
z-index:99999999;
box-sizing:border-box;
box-shadow:0 1px 8px rgba(0,0,0,0.5);
display:none;
}
.tooltip:hover .right {
display:block;
}
.tooltip .right i {
position:absolute;
top:50%;
right:100%;
margin-top:-12px;
width:12px;
height:24px;
overflow:hidden;
}
.tooltip .right i::after {
content:'';
position:absolute;
width:12px;
height:12px;
left:0;
top:50%;
transform:translate(50%,-50%) rotate(-45deg);
background-color:#EEEEEE;
box-shadow:0 1px 8px rgba(0,0,0,0.5);
}
}
`;
const App = () => {
return (
<Tooltip>...</Tooltip>
)
}
We used the styled.div
template tag, which is a function that takes CSS styles and returns a component with the styles.
Then we can use them in our component as we did in App
.
Functions in Stateless Components
We can call functions that are outside of the component within the component.
For instance, we can write:
const App = props => (
<Something onClick={props.update} />
);
We can get functions from props and call them.
We can also have functions inside the component.
For instance, we can write:
const App = props => {
const onClick = useCallback((a, b) => {
//...
}, [props.x]);
return (
<Something onClick={onClick} />
);
}
We use the useCallback
gook to ensure that functions are only redefined when one of the dependencies in the array changes.
Calling super() in a React Component Constructor
We call super
inside the constructor if we have to use this.props
.
For instance, we writ:
class App extends React.component{
constructor(props){
super(props);
console.log(this.props);
}
}
So that we get access to this.props
in our component’s code.
Subclasses must call super
before referencing this
either simplicity or explicitly.
Conclusion
We can use libraries to incorporate pseudo-classes in our React component code.
Also, we’ve to call super
before using this
in class components.
Items can be conditionally displayed with boolean expressions.