Your code is always consistent, as is the code from the rest of your team. No more bikeshedding!
Lets install it either locally npm i -D prettier or run it with npx prettier.
Prettier is heavily opinionated. Luckily there are a lot of options to customize it to your liking
Add a prettier.config.js file to your project
module.exports = {
singleQuote: true,
trailingComma: 'all',
quoteProps: 'consistent',
}
Add a npm script to the package.json.
"scripts": {
"pretty": "prettier --write \"src/**/*.{js,json,
yml,yaml,css,scss,ts,tsx,md}\"",
},
Enforce some code styles which pertain more to usage.
Run npm i -D eslint eslint-config-prettier to install it.
"scripts": {
"lint": "eslint \"src/**/*.{js,ts,tsx}\" --quiet",
},
And again add the plugin for your IDE: VS Code
ESLint has a ton of (predefined) rules, ranging from very loos to super strict (airbnb).
Create React App comes with some predefined rules.
React has wonderful dev tools that the core team maintains.
They're available for both Chromium-based browsers and Firefox
Reads state from the URL and syncs back to it.
And as always it's components all the way (and some hooks).
const App = () => (
<Router>
<Header />
<Switch>
<Route exact path="/register">
<Register />
</Route>
<Route exact path="/quiz"">
<PokeQuiz />
</Route>
<Redirect from="/" to="register" />
</Switch>
</Router>
);
const Header = () => (
<div>
<Link to="/">Register</Link>
<Link to="/quiz">Quiz</Link>
</div>
);
gives you access to the history instance that you may use to navigate.
think about it like a useState that returns a new location whenever the URL changes.
returns an object of key/value pairs of URL parameters.
By using this Hook, you tell React that your functional component needs to perform some side effects after render.
useEffect(() => {
console.log("Hello everyone")
});
useEffect(() => {
console.log(`Hello ${name}`)
}, [name]);
useEffect(() => {
console.log("Hello everyone")
return () => {
console.log("Goodbye")
}
}, []);
const PokeQuiz = () => {
const [pokemonList, setPokemonList] = useState([]);
const URL = 'https://pokeapi.co/api/v2/pokemon?limit=50';
useEffect(() => {
(async () => {
const response = await fetch(URL);
const pokemonList = response.json().results;
setPokemonList(pokemonList);
})();
}, []);
return pokemonList.map(pokemon => {pokemon.name});
};
Building your own Hooks lets you extract component logic into reusable functions.
Enable hard mode with /quiz?level=pokemaster
const useQuery = () =>
new URLSearchParams(useLocation().search);