Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
leobastiani committed Jan 17, 2024
1 parent a7d29e6 commit ee17a96
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

- [Getting Started](#getting-started)
- [Usage example](#usage-example)
- [Hello World](#hello-world)
- [Example with react-query](#example-with-react-query)
- [Clock](#clock)

Expand All @@ -37,6 +38,28 @@ yarn add react-with-hoc

## Usage example

### Hello World

```tsx
import { withDefault, withOverride, withHocs } from "react-with-hoc";

export const Hello = (() => {
function Hello({ name }: { name: string }) {
return <div>Hello {name}!</div>;
}

return withHocs([withDefault({ name: "World" })])(Hello);
})();

// <Hello /> is equivalent to <div>Hello World!</div>
// <Hello name="You" /> is equivalent to <div>Hello You!</div>

export const HelloYou = withOverride("name", "You")(Hello);

// <HelloYou /> is equivalent to <div>Hello You!</div>
// <HelloYou name="..." /> is typescript error ❌
```

### Example with react-query

Lets suppose you have the following code and then you need a query inside your App component
Expand Down Expand Up @@ -84,6 +107,29 @@ export default withWrapper(
// export default withWrapper(MyQueryClientProvider)(App);
```

Using [IIFE](https://developer.mozilla.org/pt-BR/docs/Glossary/IIFE)

```tsx
import { withWrapper, withOverride, withHocs } from "react-with-hoc";

const queryClient = new QueryClient();

const App = (() => {
function App() {
//
const query = useQuery({...});

return <>...</>;
}

return withHocs([
withWrapper(withOverride({ client: queryClient })(QueryClientProvider)),
])(App);
})();

export default App;
```

### Clock

- [Demo](https://leobastiani.github.io/react-with-hoc/example/)
Expand Down
8 changes: 6 additions & 2 deletions src/hocs/withHocs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ type WithHocsFlat<
* Apply multiple hocs
*
* @example
* const NewComponent = withHocs([withWrapper(...), withWrapper(...)])(MyComponent)
* <NewComponent ... />
* const NewComponent = withHocs([withWrapper(A), withWrapper(B)])(MyComponent)
* <A>
* <B>
* <NewComponent ... />
* </B>
* </A>
*
* @example
* const MyComponent = (() => {
Expand Down

0 comments on commit ee17a96

Please sign in to comment.