Skip to main content

@resolve-js/react-hooks

The @resolve-js/react-hooks library exposes React hooks that you can use to connect React components to a reSolve backend. The following hooks are included:

HookDescription
useClientReturns the @resolve-js/client library's client object.
useCommandInitializes a command that can be passed to the backend.
useCommandBuilderAllows a component to generate commands based on input parameters.
useViewModelEstablishes a WebSocket connection to a reSolve View Model.
useQueryAllows a component to send queries to a reSolve Read Model or View Model.
useQueryBuilderAllows a component to generate queries based on input parameters.
useOriginResolverResolves a relative path to an absolute URL within the application.
useStaticResolverResolves a relative path to a static resource's full URL.

useClientā€‹

Returns the @resolve-js/client library's client object.

Exampleā€‹

const client = useClient()

client.command(
{
aggregateName: 'Chat',
type: 'postMessage',
aggregateId: userName,
payload: message,
},
(err) => {
if (err) {
console.warn(`Error while sending command: ${err}`)
}
}
)

useCommandā€‹

Initializes a command that can be passed to the backend.

Exampleā€‹

const ShoppingList = ({
match: {
params: { id: aggregateId }
}
}) => {
const renameShoppingList = useCommand({
type: 'renameShoppingList',
aggregateId,
aggregateName: 'ShoppingList',
payload: { name: shoppingList ? shoppingList.name : '' }
})

...

const onShoppingListNamePressEnter = event => {
if (event.charCode === 13) {
event.preventDefault()
renameShoppingList()
}
}

...
}

useCommandBuilderā€‹

Allows a component to generate commands based on input parameters.

Exampleā€‹

const ShoppingList = ({
match: {
params: { id: aggregateId }
}
}) => {
const clearItemText = () => setItemText('')

const createShoppingItem = useCommandBuilder(
text => ({
type: 'createShoppingItem',
aggregateId,
aggregateName: 'ShoppingList',
payload: {
text,
id: Date.now().toString()
}
}),
clearItemText
)

...

const onItemTextPressEnter = event => {
if (event.charCode === 13) {
event.preventDefault()
createShoppingItem(itemText)
}

...
}

useViewModelā€‹

Establishes a WebSocket connection to a reSolve View Model.

Exampleā€‹

const ShoppingList = ({
match: {
params: { id: aggregateId }
}
}) => {
const [shoppingList, setShoppingList] = useState({
name: '',
id: null,
list: []
})

const { connect, dispose } = useViewModel(
'shoppingList',
[aggregateId],
setShoppingList
)

useEffect(() => {
connect()
return () => {
dispose()
}
}, [])

...

const updateShoppingListName = event => {
setShoppingList({ ...shoppingList, name: event.target.value })
}

...
}

useQueryā€‹

Allows a component to send queries to a reSolve Read Model or View Model.

Exampleā€‹

const MyLists = () => {
const getLists = useQuery(
{ name: 'ShoppingLists', resolver: 'all', args: {} },
(error, result) => {
setLists(result)
}
)

useEffect(() => {
getLists()
}, [])

...
}

useQueryBuilderā€‹

Allows a component to generate queries based on input parameters.

Exampleā€‹

const getPage = useQueryBuilder(
(page) => ({
name: 'MessageList',
resolver: 'paginated',
args: { page },
}),
(error, result) => {
setMessages(result.data)
}
)

useEffect(() => {
getPage(page)
}, [])

useOriginResolverā€‹

Resolves a relative path to an absolute URL within the application.

Exampleā€‹

var resolver = useOriginResolver()
var commandApiPath = resolver('/api/commands')

useStaticResolverā€‹

Resolves a relative path to a static resource's full URL.

var staticResolver = useStaticResolver()
var imagePath = staticResolver('/account/image.jpg')