 # 30 seconds of React > Curated collection of useful React snippets that you can understand in 30 seconds or less. - Use Ctrl + F or command + F to search for a snippet. - Contributions welcome, please read the [contribution guide](CONTRIBUTING.md). - Snippets are written in React 16.8+, using hooks. ### Prerequisites To import a snippet into your project, you must import `React` and copy-paste the component's JavaScript code like this: ```js import React from 'react'; function MyComponent(props) { /* ... */ } ``` If there is any CSS related to your component, copy-paste it to a new file with the same name and the appropriate extension, then import it like this: ```js import './MyComponent.css'; ``` To render your component, make sure there is a node with and id of `"root"` present in your element (preferrably a `
| ID | Value |
|---|---|
| {i} | {val} |
| ` element. * Use `Array.prototype.map` to render each object in the `filteredData` array as a ` | ||
|---|---|---|
` for each key in the object.
*This component does not work with nested objects and will break if there are nested objects inside any of the properties specified in `propertyNames`*
```jsx
function MappedTable({ data, propertyNames }) {
let filteredData = data.map(v =>
Object.keys(v)
.filter(k => propertyNames.includes(k))
.reduce((acc, key) => ((acc[key] = v[key]), acc), {})
);
return (
Examples```jsx const people = [ { name: 'John', surname: 'Smith', age: 42 }, { name: 'Adam', surname: 'Smith', gender: 'male' } ]; const propertyNames = ['name', 'surname', 'age']; ReactDOM.render([⬆ Back to top](#contents) --- ## Hooks ### ClickInside and ClickOutside Two handy hooks to handle the click outside and inside event on the wrapped component. * Create customized hooks that take in a `ref` component(node) and a `callback` function to hanlde the customized `click` event * Use the `React.useEffect()` hook to append and clean up the `click` event. * Use the `React.useRef()` hook to create a `ref` for your click component and pass it to `useClickInside` and `useClickOutside` hooks. ```css .click-box { border: 2px dashed orangered; height: 200px; width: 400px; display: flex; justify-content: center; align-items: center; } p { border: 2px solid blue; padding: 16px; } ``` ```jsx const useClickInside = (ref, callback) => { const handleClick = e => { //use the node contains to verify if we click inside if (ref.current && ref.current.contains(e.target)) { callback(); } }; //clean up using useEffect useEffect(() => { document.addEventListener("click", handleClick); return () => { document.removeEventListener("click", handleClick); }; }); }; const useClickOutside = (ref, callback) => { const handleClick = e => { //use the node contains to verify if we click outside if (ref.current && !ref.current.contains(e.target)) { callback(); } }; // clean up using useEffect useEffect(() => { document.addEventListener("click", handleClick); return () => { document.removeEventListener("click", handleClick); }; }); }; function ClickBox({onClickOutside,onClickInside}) { const clickRef = useRef(); useClickOutside(clickRef, onClickOutside); useClickInside(clickRef, onClickInside); return ( Hello Click Me Inside! Examples```jsx ReactDOM.render([⬆ Back to top](#contents) --- ## Input ### LimitedTextarea Renders a textarea component with a character limit. * Use the `React.useState()` hook to create the `content` state variable and set its value to `value`. Create a method `setFormattedContent`, which trims the content of the input if it's longer than `limit`. * Use the `React.useEffect()` hook to call the `setFormattedContent` method on the value of the `content` state variable. * Use a` ` to wrap both the` |