-
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.
- Loading branch information
Showing
7 changed files
with
98 additions
and
19 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
29 changes: 29 additions & 0 deletions
29
src/routes/state-management/_components/CounterExample.tsx
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,29 @@ | ||
import { useState } from 'react'; | ||
import {Button, MarigoldProvider, Stack} from '@marigold/components'; | ||
import theme from '@marigold/theme-core'; | ||
|
||
const MyButton = () => { | ||
const [count, setCount] = useState(0); | ||
|
||
function handleClick() { | ||
setCount(count + 1); | ||
} | ||
|
||
return ( | ||
<Button onPress={handleClick} size='small'> | ||
Clicked {count} times | ||
</Button> | ||
); | ||
} | ||
|
||
const Counter = () => { | ||
return ( | ||
<MarigoldProvider theme={theme}> | ||
<div> | ||
<MyButton /> | ||
</div> | ||
</MarigoldProvider> | ||
); | ||
} | ||
|
||
export default Counter; |
38 changes: 38 additions & 0 deletions
38
src/routes/state-management/_components/counter-example.mdx
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,38 @@ | ||
import { Block } from '@/components/Container'; | ||
|
||
<Block> | ||
<CH.Scrollycoding> | ||
```tsx CounterExample.tsx focus=6 | ||
// from ./CounterExample.tsx | ||
```` | ||
|
||
### Storing an initial value | ||
|
||
Local state is typically initialized using the useState hook. This hook takes an initial state value as an argument and returns an array containing the current state and a function to update it. | ||
|
||
**0** is the initial value for the count state on the first render. | ||
|
||
--- | ||
|
||
```tsx CounterExample.tsx focus=14 | ||
// from ./CounterExample.tsx | ||
```` | ||
### Reading the current state value | ||
The current state value can be read directly from the state variable returned by useState. This state value can then be used in the component's JSX to dynamically render content. | ||
This will display the current value of **count** in the UI. | ||
--- | ||
```tsx CounterExample.tsx focus=9 | ||
// from ./CounterExample.tsx | ||
```` | ||
|
||
### Updating a state value | ||
To update the state, we use the state update function (in this case, **setCount**). This function takes the new state value as an argument and triggers a re-render of the component with the new state. | ||
|
||
This function updates the **count** state by adding 1 to its current value. | ||
|
||
</CH.Scrollycoding> | ||
</Block> |
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,12 +1,17 @@ | ||
import { createLazyFileRoute } from '@tanstack/react-router'; | ||
|
||
import { Block } from '@/components/Container'; | ||
import Index from './index.mdx'; | ||
import Title from '@/components/Title'; | ||
|
||
export const Route = createLazyFileRoute('/state-management/')({ | ||
component: () => ( | ||
<Block> | ||
<Index /> | ||
</Block> | ||
<> | ||
<Title | ||
title="State Management" | ||
caption="Learn to structure and maintain your state effectively." | ||
image={{ url: '/floating-cogs.svg', size: '10%' }} | ||
/> | ||
<Index /> | ||
</> | ||
), | ||
}); |
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