-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from wpdas/staging
update docs
- Loading branch information
Showing
34 changed files
with
334 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Markdown } from 'alem'; | ||
|
||
const CreateDebounce = () => { | ||
const mdContent = Buffer.from(`IyMgQ3JlYXRlIERlYm91bmNlCgpDcmVhdGUgYSBkZWJvdW5jZWQgbWV0aG9kIHRvIG9idGFpbiB0aGUgZGF0YSBhZnRlciB0aGUgZGVzaXJlZCBpbnRlcnZhbC4KCmBgYHRzeAppbXBvcnQgeyBjcmVhdGVEZWJvdW5jZSB9IGZyb20gImFsZW0iOwoKY29uc3QgTXlDb21wb25lbnQgPSAoKSA9PiB7CiAgY29uc3QgW3dvcmRzLCBzZXRXb3Jkc10gPSB1c2VTdGF0ZSgiIik7Ci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ucHV0Q2hhbmdlfSB0eXBlPSJ0ZXh0IiAvPjsKYGBgCg==`, "base64").toString("utf-8"); | ||
return <Markdown text={mdContent} /> | ||
} | ||
|
||
export default CreateDebounce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## Create Debounce | ||
|
||
Create a debounced method to obtain the data after the desired interval. | ||
|
||
```tsx | ||
import { createDebounce } from "alem"; | ||
|
||
const MyComponent = () => { | ||
const [words, setWords] = useState(""); | ||
const onInputChange = createDebounce((event) => setWords(event.target.value), 1000); | ||
|
||
// This will be changed 1 sec after the user stops typing. | ||
console.log(words); | ||
return <input onChange={onInputChange} type="text" />; | ||
}; | ||
``` | ||
|
||
This feature is also available separately here: **[createDebounce](https://near.org/near/widget/ComponentDetailsPage?src=alem-lib.near/widget/createDebounce)** and can be injected like this: | ||
|
||
```jsx | ||
// SomeWidget | ||
const { createDebounce } = VM.require("alem-lib.near/widget/createDebounce"); | ||
if (!createDebounce) return ""; | ||
|
||
const [words, setWords] = useState(""); | ||
const onInputChange = createDebounce((event) => setWords(event.target.value), 1000); | ||
|
||
// This will be changed 1 sec after the user stops typing. | ||
console.log(words); | ||
|
||
return <input onChange={onInputChange} type="text" />; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,145 @@ | ||
## Create Context | ||
|
||
Similar to React's Context API, Além also provides a way to create contexts. Contexts are very helpful when you want to create a specific data structure and pass it to child components. | ||
|
||
By nature, it is not possible to manipulate provider objects at the same layer in which they are used, similar to React. | ||
|
||
**Simple Example:** | ||
|
||
```tsx | ||
// UserProvider.tsx | ||
|
||
import { createContext } from "alem"; | ||
|
||
// Interface | ||
interface UserContextProps { | ||
isAuthenticated: boolean; | ||
name: string; | ||
} | ||
|
||
const UserProvider = () => { | ||
// Create a provider using a reference key | ||
// setDefaultData: used to set initial data | ||
// updateData: used to update data | ||
const { setDefaultData, updateData } = createContext<UserContextProps>("user-provider"); | ||
|
||
// Initial / default data | ||
setDefaultData({ | ||
isAuthenticated: !!context.accountId, | ||
accountId: context.accountId, | ||
}); | ||
}; | ||
``` | ||
|
||
Using context with `useContext`: | ||
|
||
```tsx | ||
import UserProvider from "./contexts/UserProvider"; | ||
|
||
const App = () => { | ||
UserProvider(); | ||
|
||
return <ChildComponent />; | ||
}; | ||
``` | ||
|
||
```tsx | ||
import { useContext } from "alem"; | ||
|
||
interface UserContextProps { | ||
isAuthenticated: boolean; | ||
name: string; | ||
} | ||
|
||
const ChildComponent = () => { | ||
const user = useContext<ProjectsContextProps>("user-context"); | ||
|
||
console.log(user); | ||
// { isAuthenticated: true, accountId: "wendersonpires.near" } | ||
|
||
return ""; | ||
}; | ||
``` | ||
|
||
**Handling async data:** | ||
|
||
```tsx | ||
// CartProvider.tsx | ||
|
||
import { createContext, useContext, asyncFetch } from "alem"; | ||
|
||
// Interface | ||
interface CartContextProps { | ||
items: string[]; | ||
ready: boolean; | ||
|
||
// actions | ||
fetchCartItems: () => void; | ||
} | ||
|
||
const CartProvider = () => { | ||
const { setDefaultData, updateData } = createContext<CartContextProps>("cart-provider"); | ||
|
||
// Initial / default data | ||
setDefaultData({ | ||
items: [], | ||
ready: false, | ||
|
||
fetchCartItems: () => { | ||
const self = useContext<CartContextProps>("cart-context"); | ||
|
||
if (!self.ready) { | ||
asyncFetch("https://api.com/cart/items").then((response) => { | ||
// Update items | ||
updateData({ | ||
items: response.body, | ||
ready: true, | ||
}); | ||
}); | ||
} | ||
}, | ||
}); | ||
|
||
// (Optional): Auto fetch cart items | ||
const self = useContext<CartContextProps>("cart-context"); | ||
if (!self.ready) { | ||
self.fetchCartItems(); | ||
} | ||
}; | ||
``` | ||
|
||
Then you can use the data: | ||
|
||
```tsx | ||
import CartProvider from "./contexts/CartProvider"; | ||
|
||
const App = () => { | ||
CartProvider(); | ||
|
||
return <ChildComponent />; | ||
}; | ||
``` | ||
|
||
```tsx | ||
import { useContext, useEffect } from "alem"; | ||
|
||
interface CartContextProps { | ||
items: string[]; | ||
ready: boolean; | ||
fetchCartItems: () => void; | ||
} | ||
|
||
const ChildComponent = () => { | ||
const cart = useContext<CartContextProps>("cart-context"); | ||
|
||
useEffect(() => { | ||
// Fetch cart items (in case you are not using the auto fetch) | ||
cart.fetchCartItems(); | ||
}, []); | ||
|
||
console.log(cart.items); | ||
// [item1, item2, item3, ...] | ||
|
||
return ""; | ||
}; | ||
``` |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.