Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
intro-react
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
javascript
intro-react
Commits
28e7645b
Commit
28e7645b
authored
1 year ago
by
Jean-Christophe
Browse files
Options
Downloads
Patches
Plain Diff
useEffect hook à la création du composant
parent
181468d4
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+1
-1
1 addition, 1 deletion
README.md
src/components/person.component.jsx
+7
-1
7 additions, 1 deletion
src/components/person.component.jsx
src/components/personListing.component.jsx
+1
-1
1 addition, 1 deletion
src/components/personListing.component.jsx
with
9 additions
and
3 deletions
README.md
+
1
−
1
View file @
28e7645b
...
...
@@ -51,7 +51,7 @@ où `tag` peut prendre comme valeur :
*
`v5.3`
: modification et état courant : utilisation d'une fonction en argument de
`setOn`
pour modifier l'état à partir de la valeur précédente
voir
`/src/components/star.jsx`
(et
`/src/components/person.component.jsx`
)
*
`v5.5`
: cycle de vie du composant : utilisation du hook
`useEffect`
pour agir au moment de la création du composant : juste après le premier rendu.
voir
`/src/components/personListing.jsx`
voir
`/src/components/personListing.
component.jsx`
ainsi que
`/src/components/person.component.
jsx`
Faire
```git checkout main```
pour revenir à la version finale.
...
...
This diff is collapsed.
Click to expand it.
src/components/person.component.jsx
+
7
−
1
View file @
28e7645b
import
{
useState
}
from
'
react
'
;
import
{
useState
,
useEffect
}
from
'
react
'
;
// import stysheet defining style specific to this component
import
'
../assets/style/person.css
'
;
...
...
@@ -11,6 +11,12 @@ const Person = ( { name = 'Anonymous', age, started, delay } ) => {
const
[
currentAge
,
setCurrentAge
]
=
useState
(
age
);
useEffect
(
()
=>
{
if
(
started
)
{
setInterval
(
()
=>
setCurrentAge
(
previousCurrentAge
=>
previousCurrentAge
+
1
),
delay
);
}
},
[]);
return
(
<
div
className
=
"person"
>
Here is :
<
div
>
name :
<
span
>
{
name
}
</
span
>
</
div
>
...
...
This diff is collapsed.
Click to expand it.
src/components/personListing.component.jsx
+
1
−
1
View file @
28e7645b
...
...
@@ -16,7 +16,7 @@ const PersonListing = props => {
useEffect
(
()
=>
{
console
.
log
(
'
component did mount
'
);
const
fetchData
=
async
()
=>
{
const
data
=
await
mockFetch
(
'
http://source.of.data/persons
'
,
1000
);
const
data
=
await
mockFetch
(
'
http://source.of.data/persons
'
);
setPersons
(
data
);
}
fetchData
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment