Using npm:
$ npm install dstor-react-sdk
Once the package is installed, you can import the library using import
approach.
import { DstorProvider, DstorContext } from 'dstor-react-sdk';
First you need to specify Environment in the DstorProvider:
environment: "maindish" || "staging" || "production"
import { DstorProvider } from 'dstor-react-sdk';
<DstorProvider config={
{ environment: "xxx" } // "maindish" || "staging" || "production"
}
/>
import Recat, { useContext } from "react";
import { DstorProvider, DstorContext } from 'dstor-react-sdk';
const credits = {
email: 'xxxxxxx@xxx.xxx',
password: 'xxxxxxx'
}
const SignIn = () => {
const { dstorAuthentication } = useContext(DstorContext);
const dstorAuthenticationWithEmailPassword = async () => {
return await dstorAuthentication(credits); // return: "Authentication successful!"
}
return (
<button onClick={dstorAuthenticationWithEmailPassword}>Sign In</button>
)
}
const App = () => {
return (
<DstorProvider config={{ environment: "xxx" }}> // "maindish" || "staging" || "production"
<SignIn />
</DstorProvider>
)
}
React.DOM.render(<App />, document.getElementById("root"));
import Recat, { useContext } from "react";
import { DstorProvider, DstorContext } from 'dstor-react-sdk';
const credits = {
apiKey: 'xxxxxxxxxxxxxxxx'
}
const SignIn = () => {
const { dstorAuthentication } = useContext(DstorContext);
const dstorAuthenticationWithApiKey = async () => {
return await dstorAuthentication(credits); // return: "Authentication successful!"
}
return (
<button onClick={dstorAuthenticationWithApiKey}>Sign In</button>
)
}
const App = () => {
return (
<DstorProvider config={{ environment: "xxx" }}> // "maindish" || "staging" || "production"
<SignIn />
</DstorProvider>
)
}
React.DOM.render(<App />, document.getElementById("root"));
import Recat, { useContext, useState, useEffect } from "react";
import { DstorProvider, DstorContext } from 'dstor-react-sdk';
const GetApiBase = () => {
const { dstorApiBase } = useContext(DstorContext);
const [ apiBase, setApiBase ] = useState();
useEffect(() => {
setApiBase(dstorApiBase); // api.xxx.Dstor.cloud
}, [])
return (
<div>{ apiBase }</div>
)
}
const App = () => {
return (
<DstorProvider config={{ environment: "xxx" }}> // "maindish" || "staging" || "production"
<GetApiBase />
</DstorProvider>
)
}
React.DOM.render(<App />, document.getElementById("root"));
hash: hash that belongs to the file on dStor
import Recat, { useContext, useState, useEffect } from "react";
import { DstorProvider, DstorContext } from 'dstor-react-sdk';
const hash = 'xxxxxxxxxxx'
const GetFileInfo = () => {
const { dstorGetFileInfo } = useContext(DstorContext);
const [ fileInfo, setFileInfo ] = useState();
const getFileInfo = async () => {
return await dstorGetFileInfo(hash);
}
useEffect(() => {
setFileInfo(getFileInfo); // [{file_name: ... , file_hash: ... , file_size: ... , ......}]
}, [])
return (
<div>{ fileInfo }</div>
)
}
const App = () => {
return (
<DstorProvider config={{ environment: "xxx" }}> // "maindish" || "staging" || "production"
<GetFileInfo />
</DstorProvider>
)
}
React.DOM.render(<App />, document.getElementById("root"));
data: e.target.files (or array of files)
import Recat, { useContext, useState, useEffect } from "react";
import { DstorProvider, DstorContext } from 'dstor-react-sdk';
const UploadFile = () => {
const { dstorUploadFile } = useContext(DstorContext);
const uploadFile = async (e) => {
return await dstorUploadFile(e.target.files); // return: Upload successfully!
}
return (
<input type="file" onChange={uploadFile} multiple />
)
}
const App = () => {
return (
<DstorProvider config={{ environment: "xxx" }}> // "maindish" || "staging" || "production"
<UploadFile />
</DstorProvider>
)
}
React.DOM.render(<App />, document.getElementById("root"));