Skip to content

Commit 2065e51

Browse files
authored
Adding React example
1 parent 2d7a85b commit 2065e51

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The duration of the animation in milliseconds, defaults to 0 for no animation.
3636
default is ease. Possible values: `ease|easeIn|easeOut|easeInOut|linear`
3737

3838
## Examples
39+
### Vanilla JS
3940

4041
```javascript
4142
import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
@@ -51,3 +52,42 @@ scrollIntoViewIfNeeded(activeNode, false, {
5152
})
5253

5354
```
55+
56+
### React
57+
58+
```javascript
59+
import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
60+
import { Component } from 'react'
61+
62+
export default class Homepage extends Component {
63+
64+
constructor(props) {
65+
super(props)
66+
67+
this.setSignupNode = (node) => {
68+
if(node) {
69+
this._setupNode = node
70+
}
71+
}
72+
this.goToSignup = (event) => {
73+
event.preventDefault()
74+
75+
// Passing the dom node from react is all you need for this to work
76+
scrollIntoViewIfNeeded(this._signupNode, false, {
77+
duration: 150
78+
})
79+
}
80+
}
81+
82+
render() {
83+
return (
84+
...
85+
<a onClick={this.goToSignup}>Signup Now!</a>
86+
...
87+
<form ref={this.setSignupNode}>
88+
...
89+
)
90+
}
91+
}
92+
93+
```

0 commit comments

Comments
 (0)