This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import ComponentB from "./componentB"; | |
const someArbitaroryObj = { | |
name: 'object has no name', | |
someArray: [ | |
{ | |
id: 'aman', | |
color: "red", | |
value: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { object, array, shape, string, arrayOf, number } from "prop-types"; | |
// inside ComponentB | |
static propTypes = { | |
values: shape({ | |
name: string, | |
someArray: arrayOf( | |
shape({ | |
id: number.isRequired, // if passed props doens't have this id, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { arrayOf, shape, number } from "prop-types"; | |
import { map } from "lodash"; | |
const ComponentC = props => { | |
const list = map(props.list, el => { | |
return ( | |
<h3 key={el.id}> | |
color: {el.color} --- value: {el.value.nestedValue} | |
</h3> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import Rx from "rxjs/Rx"; | |
export default class Reactive extends React.PureComponent { | |
state = { | |
subject: null, | |
subcription: null, | |
value: 0 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class RenderProps extends Component { | |
/* | |
Encapsulate your logic here... | |
*/ | |
render(){ | |
// call the functional props by passing the data required to render UI | |
return this.props.render(this.state) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class GiantComponent extends Component { | |
componentDidMount(){ | |
//side effects | |
this.makeRequest(); | |
document.addEventListener('...'); | |
this.timerId = startTimer(); | |
// more ... | |
} | |
componentdidUpdate(prevProps){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function HOC(WrappedComponent){ | |
return class EnhancedComponent extends Component { | |
/* | |
Encapsulate your logic here... | |
*/ | |
// render the UI using Wrapped Component | |
render(){ | |
return <WrappedComponent {...this.props} {...this.state} /> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
export default function MouseTracker() { | |
// useState accepts initial state and you can use multiple useState call | |
const [mouseX, setMouseX] = useState(25); | |
const [mouseY, setMouseY] = useState(25); | |
return ( | |
<div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
export default function MouseTracker() { | |
// useState accepts initial state and you can use multiple useState call | |
const [mouseX, setMouseX] = useState(25); | |
const [mouseY, setMouseY] = useState(25); | |
function handler(event) { | |
const { clientX, clientY } = event; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// you can write your custom hooks in this file | |
import { useState, useEffect } from 'react'; | |
export function useMouseLocation() { | |
const [mouseX, setMouseX] = useState(25); | |
const [mouseY, setMouseY] = useState(25); | |
function handler(event) { | |
const { clientX, clientY } = event; | |
setMouseX(clientX); |
OlderNewer