Skip to content

Commit

Permalink
docs(readme): added useClassNames
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Jan 9, 2024
1 parent f4c6863 commit 06b0cbf
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,33 @@

Collection of react utilities curated by the Rainbow Cafe~

## `useCachedState` and `useReadOnlyCachedState`
## `useCachedState` / `useReadOnlyCachedState` / `useClassNames`

```tsx
import {
useCachedState,
useReadOnlyCachedState,
} from '@rain-cafe/react-utils';
import { useCachedState, useReadOnlyCachedState, useClassNames, classNames } from '@rain-cafe/react-utils';
import classnames from 'classnames';
import * as styles from './MySimpleInput.module.scss';

export type MySimpleInputProps = {
className?: string;
value?: string;
}
};

export function MySimpleInput({ className: externalClassName, value: externalValue }: MySimpleInputProps) {
// This is a utility for keeping external properties in-sync with the internal state
const [value, setValue] = useCachedState(() => externalValue, [externalValue]);

// This is a utility for computing properties only when changes occur

// Longer Version
const className = useReadOnlyCachedState(() => {
return classnames(styles.input, externalClassName);
return classNames(styles.input, externalClassName);
}, [externalClassName]);

return (
<input
className={className}
value={value}
onChange={(event) => setValue(event.target.value)}
/>
);
// Short-hand Version
const className = useClassNames([styles.input, externalClassName]);

return <input className={className} value={value} onChange={(event) => setValue(event.target.value)} />;
}
```

Expand Down

0 comments on commit 06b0cbf

Please sign in to comment.