Skip to content

Repository files navigation

@nosplatform/api-functions

nOS API bindings and types


installation

npm i --save @nosplatform/api-functions
yarn add @nosplatform/api-functions

Usage in react

HoC

Wrap your component with the higher-order components to provide fallbacks when running outside the context of the nOS client. Specify propTypes provided by this package.

importReactfrom'react';import{compose}from'recompose';import{injectNOS,injectAssets,nosProps,assetProps}from"@nosplatform/api-functions/lib/react";classShowBalanceextendsReact.Component{staticpropTypes={nos: nosProps.isRequired,assets: assetProps.isRequired}render(){return(<buttontype="button"onClick={this.handleClick}>
Show NEO Balance
</button>);}asynchandleClick=()=>{const{ nos, assets }=this.props;constbalance=awaitnos.getBalance({asset: assets.NEO});console.log('NEO Balance:',balance);}};exportdefaultcompose(injectNOS,injectAssets)(ShowBalance);

Render Props

importReactfrom'react';import{NosAssets,NosFunctions}from"@nosplatform/api-functions/lib/react";constShowBalance=()=>{render(){consthandleClick=async(nos,assets)=>{constbalance=awaitnos.getBalance({asset: assets.NEO});console.log('NEO Balance:',balance);}return(<NosFunctions>{({ nos })=>(<NosAssets>{({ assets })=>(<buttontype="button"onClick={()=>handleClick(nos,assets)}>
Show NEO Balance
</button>)}</NosAssets>)}</NosFunctions>);}};exportdefaultShowBalance;

Hooks

importReactfrom'react';import{compose}from'recompose';import{useNOS,useAssets}from"@nosplatform/api-functions/lib/react";constShowBalance=()=>{render(){constnos=useNOS();constassets=useAssets();consthandleClick=async()=>{constbalance=awaitnos.getBalance({asset: assets.NEO});console.log('NEO Balance:',balance);}return(<buttontype="button"onClick={handleClick}>
Show NEO Balance
</button>);}};exportdefaultShowBalance;

In addition to automatically providing the NOS API function as a prop to your React component, the api-functions package also provides the opportunity to specify a fallback implementation. This is especially useful for building in the context of another browser if not wanting to use the nOS client for any reason.

constpreviousBalance="23";// Calculated previous balanceconstbalance=awaitnos.getBalance({asset: assets.NEO},()=>Promise.resolve(previousBalance));console.log("NEO Balance:",balance);// NEO Balance: 23

About

nOS API bindings and typings

Resources

Code of conduct

Contributing

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages