A simple technique for sharing code between React components using a prop where its value is a function
<Header
import logo from './logo.svg'; | |
// hocs | |
import reaquireAuth from './requireAuth'; | |
/* | |
DECORATOR - (@) | |
- Cannot use them in create-react-app, unless inject them | |
- Support is wonky, spec is not final | |
- Can export only the decorated component | |
*/ |
function handlePopState(e) {
// code before url change
}
window.addEventListener('popstate', handlePopState);
Reusable, reliable solutions that we face everyday in software development.
One is always tasked with a hurdle to solve a recurring problem over and over again, where we (the developers) challange ourselves to solve the problem once and for all, aka the solution. Therefore, the primary goal of design patterns are to encourage programming efficiency and code reuse.
If you find your solution to work time and time again, it is safe to share, evangelise, and document your solution and make it official.
Often in a JS project we import a 3rd party library in order to provide a solution to a problem we have encountered, however there are cases where a library does not provide the full specific functionality that we have to achieve when working on a project.
In production scenarios, we will include libraries for testing and database tools such as puppeteer, and mongoose.
This is the most simplest solution out there to achieve and we will take mongoose for an example case study.
var lines = { | |
B: 'Bakerloo', | |
C: 'Central', | |
D: 'District', | |
H: 'Hammersmith & Circle', | |
J: 'Jubilee', | |
M: 'Metropolitan', | |
N: 'Northern', | |
P: 'Piccadilly', | |
V: 'Victoria', |
import React, { Component } from 'react'; | |
import { connect } from 'react-redux'; | |
import { fetchAdmins } from '../actions'; | |
import requireAuth from '../components/hocs/requireAuth'; | |
class AdminsListPage extends Component { | |
componentDidMount() { | |
this.props.fetchAdmins(); | |
} |