Skip to content

Commit b54d50f

Browse files
committed
documents the useMessageSource hook API
1 parent 5898a0d commit b54d50f

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ An optional fetching lifecycle method. Invoked just after `GET /props.url` respo
4545
##### `onFetchingError?: (e?: Error) => void`
4646
An optional fetching lifecycle method. Invoked when error occurs during fetching/processing stage.
4747

48+
## `useMessageSource(keyPrefix?: string): ComponentAPI`
49+
A React Hook version of the [ComponentAPI](#ComponentApi).
50+
51+
```js
52+
import React from 'react'
53+
import { useMessageSource } from 'react-message-source'
54+
55+
function MyComponent() {
56+
const { getMessage, getMessageWithNamedParams } = useMessageSource()
57+
return ...
58+
}
59+
```
60+
4861
## `withMessages`
4962
Creates a higher order component and provides the [ComponentAPI](#ComponentAPI) as `props`. It can be used in two ways:
5063

@@ -102,7 +115,7 @@ Exposes the [ComponentAPI](#ComponentApi) as standard `prop-types` definition.
102115
#### `App.jsx`
103116
```jsx
104117
import React, { Component } from 'react'
105-
import * as MessageSource from 'react-message-source'
118+
import { Provider as MessageSourceProvider } from 'react-message-source'
106119

107120
import translations from './translations.json'
108121

@@ -112,31 +125,33 @@ import MyComponentWithNamedParams from './MyComponentWithNamedParams'
112125

113126
export default function App() {
114127
return (
115-
<MessageSource.Provider value={translations}>
128+
<MessageSourceProvider value={translations}>
116129
<MyComponent />
130+
<MyComponentWithHooks />
117131
<MyComponentWithIndexedParams />
118132
<MyComponentWithNamedParams />
119-
</MessageSource.Provider>
133+
</MessageSourceProvider>
120134
)
121135
}
122136
```
123137

124138
#### `FetchApp.jsx`
125139
```jsx
126140
import React, { Component } from 'react'
127-
import * as MessageSource from 'react-message-source'
141+
import { FetchingProvider as FetchingMessageSourceProvider } from 'react-message-source'
128142

129143
import MyComponent from './MyComponent'
130144
import MyComponentWithIndexedParams from './MyComponentWithIndexedParams'
131145
import MyComponentWithNamedParams from './MyComponentWithNamedParams'
132146

133147
export default function FetchApp() {
134148
return (
135-
<MessageSource.FetchingProvider url="http://api.myapp.com/intl?lang=en">
149+
<FetchingMessageSourceProvider url="http://api.myapp.com/intl?lang=en">
136150
<MyComponent />
151+
<MyComponentWithHooks />
137152
<MyComponentWithIndexedParams />
138153
<MyComponentWithNamedParams />
139-
</MessageSource.FetchingProvider>
154+
</FetchingMessageSourceProvider>
140155
)
141156
}
142157
```
@@ -154,6 +169,17 @@ function MyComponent(props) {
154169
export default withMessages(MyComponent)
155170
```
156171

172+
#### `MyComponentWithHooks.jsx`
173+
```jsx
174+
import React from 'react'
175+
import { useMessageSource } from 'react-message-source'
176+
177+
export default function MyComponent(props) {
178+
const { getMessage } = useMessageSource();
179+
return <span>{getMessage('hello.world')}</span>
180+
}
181+
```
182+
157183
#### `MyComponentWithIndexedParams.jsx`
158184
```jsx
159185
import React from 'react'
@@ -187,4 +213,4 @@ export default compose(
187213

188214
## License
189215

190-
MIT © [Netcetera AG](https://github.com/netceteragroup)
216+
MIT © [Netcetera](https://github.com/netceteragroup)

0 commit comments

Comments
 (0)