Skip to content
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

#React-native-updation, updated readme #18

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"presets": [
"react",
"env",
"stage-0"
]
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
package-lock.json
webpack.config.js
.idea/vcs.xml
node-moduels/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.babelrc
node-moduels/
.git
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ I based this off: https://github.com/joshwnj/react-visibility-sensor with some s
Assuming you already setup your component, here's a quick example.

```
import InViewPort from "react-native-inviewport";
checkVisible = (isVisible) => {
if(isVisible){
if(!this.state.visible){
Expand Down Expand Up @@ -59,3 +60,71 @@ render() {
);
}
```

### Configurable props
* [delay](#delay)
* [disabled](#disabled)


### Event props
* [onChange] (#onChange)

### Configurable props
#### delay
A number that indicates milliseconds of periodically delay to check component visibility. By default it is 100ms.
Example:
```
<InViewPort delay={1000}>
...
</InViewPort>
```


#### disabled
InviewPort always keep checking the component with a delay of 100ms. This props can be used for disabling checking for component visibility.
Example:
```
<InViewPort disabled={true}>
...
</InViewPort>
```

### Event props
#### onChange
Callback function that is called whenever the component visibility change. It returns `true` or `false`.
Example:
```
const onChangeVisibility = isVisible => console.log('is component visible ', isVisible);
<InViewPort onChange={onChangeVisibility}>
...
</InViewPort>
```


## Other Examples:
Let's assume you want to check whether a component at scroll end has displayed or not and it is rendering components asynchronise on scrollview so you can use `onScroll` event listener on scrollview.
Example
```
import InViewPort from "@coffeebeanslabs/react-native-inviewport";

this.state = {visible: false, isScrolling: false};

checkVisible = isVisible => this.setState({visible: isVisible});
const onScroll = () => this.setState({isScrolling: true});

render() {
return (
<ScrollView style={{flex: 1}} onScroll={onScroll}>
//list of images
//<Image..... />
//.......

{isScrolling && <InViewPort onChange={(isVisible) => this.checkVisible(isVisible)}>
<View style={{flex: 1, height: 200, backgroundColor: 'blue'}}>
<Text style={{color: 'white'}}>View is visible? {this.state.visible}</Text>
</View>
</InViewPort>}
</ScrollView>
);
}
```
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React, { Component } from 'react'
import { View, NativeMethodsMixin, Dimensions } from 'react-native'

exports.InViewPort = class extends Component {
export default class InViewPort extends Component {
constructor(props) {
super(props)
this.state = { rectTop: 0, rectBottom: 0 }
Expand All @@ -19,7 +19,7 @@ exports.InViewPort = class extends Component {
this.stopWatching()
}

componentWillReceiveProps(nextProps) {
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.disabled) {
this.stopWatching()
} else {
Expand Down