0.0.1 • Published 4 years ago

use-blockstack v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

useBlockstack

API

This package exports two different objects, a React Component and a React Hook.

The Blockstack component is intended to provide context to the hook, and allow it to read the current session and update whatever component needs updating.

The useBlockstack hook enables you to use simple and common Blockstack functions to run in your app.

<Blockstack>

This component should be placed at the root of your app or close to it

This component provides the necessary context to every useBlockstack hook your app uses. It must be placed over any component that calls useBlockstack.

It has no props besides its children and needs no configuration.

Example:

function Login() {
	const [, { login } ] = useBlockstack();

	return <Button onClick={login}>Login</Button>
}

function App() {
	return (
		<Blockstack>
			<Login/>
		</Blockstack>
	);
}

useBlockstack

tl;dr

function Container() {
	const [ session, { putFile, getFile, login, logout } ] = useBlockstack();

	if(!session) {
		return <Login/>;
	}

	return <span onClick={logout}> Welcome {session.username} </span>;
}

More info on the returned params:

ParamTypeDescription
sessionUserDatanull if user is signed out, else UserData.
putFile(name, data)functionAllows to write a file to Gaia storage via Blockstack
getFile(name)functionAllows to read a file from Gaia storage via Blockstack
loginfunctionRedirects the user to login
logoutfunctionLogs the current user out

login

login redirects the current user to the Blockstack login page. After redirection the hook removes the search string from the current URL.

This may be revised in future versions, allowing for custom search returns

If using multiple useBlockstack hooks it is possible that every hook will change context, thus rendering multiple times. Will be revised.