Sometimes it can be very useful to save previous values of props (or state) in a React component. Perhaps this is easier to be imagined in a class component, but in a functional component, less so. However, we can use the useRef() React hook to achieve that quite efficiently. Less talk, more code (typescript)… const someFunctionalComponent = (p: {index: number}) => { // Keep the last positive value around. const
Continue readingTag: typescript
Using enum values as strictly typed object keys
In TypeScript, it’s often useful to define interfaces or complex (structured) types whose properties (or keys) may only be values of a previously defined enum type. Here’s a good example: an object declaring a set of buttons for a modal dialog. Instead of this… type DialogButtons = { yes?: boolean, no?: boolean, cancel?: boolean } interface IDialog { buttons: DialogButtons } const dialog: IDialog = { buttons: { yes: true,
Continue reading