Skip to content

added basic example of useState hook in functional component #2842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions content/docs/hooks-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ function Example(props) {
return <div />;
}
```
**Basic Example of useStae hook in functional component**

```js
import react,{useState} from "react"
function App(){
const [answer]=useState("yes lovely") //useState hooks used here
return(
<div>
<h1>Are you coming{answer}</h1>
</div>
)
}
export default App
```

You might have previously known these as "stateless components". We're now introducing the ability to use React state from these, so we prefer the name "function components".

Expand All @@ -106,6 +120,7 @@ function Example() {
>
>There are some special rules about where you can and can't use Hooks within a component. We'll learn them in [Rules of Hooks](/docs/hooks-rules.html).


## Declaring a State Variable {#declaring-a-state-variable}

In a class, we initialize the `count` state to `0` by setting `this.state` to `{ count: 0 }` in the constructor:
Expand Down