Skip to main content

Posts

Showing posts from April, 2020

The Problem with React's Context API

The problem with context is simple:  Everything that consumes a context re-renders everytime that context’s state changes. That means that if you’re consuming your context all over the place in your app, or worse, using one context for your entire app’s state, you’re causing a ton of re-renders all over the place! https://leewarrick.com/blog/the-problem-with-context/ function App() {   return (     <div>       <h2>No Unnecessary Re-renders! 😎</h2>       <MessageProvider>         <Message />         <Message />         <Message />       </MessageProvider>       <CountProvider>         <Count />       </CountProvider>     </div>   ) } render(App)

DND for react - unstated-next

change photos orders https://codesandbox.io/s/github/react-dnd/react-dnd/tree/gh-pages/examples_hooks_ts/04-sortable/simple?from-embed=&file=/src/example.tsx dropzone for upload file https://github.com/react-dropzone/react-dropzone sample of DND https://github.com/learnwithparam/logrocket-drag-and-drop/blob/master/src/ImageList.js DND for listview https://blog.logrocket.com/drag-and-drop-in-react/ 200 bytes to never think about React state management libraries ever again https://github.com/jamiebuilds/unstated-next

React.useState does not reload state from props

The argument passed to useState is the initial state much like setting state in constructor for a class component and isn't used to update the state on re-render If you want to update state on prop change, make use of  useEffect  hook function Avatar ( props ) { const [ user , setUser ] = React . useState ({... props . user }); React . useEffect (() => { setUser ( props . user ); }, [ props . user ]) return user . avatar ? (< img src ={ user . avatar }/>) : (< p > Loading ...</ p >); } const [ images , setImages ] = useState < Photo []>( photos ) useEffect (() => { setImages ( photos ) }, [ photos ])