Skip to content
Open
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
35 changes: 26 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import React from "react";
import { StyleSheet, Text, View } from "react-native";

const initialState = {
measured: false,
shouldShowReadMore: false,
showAllText: false
};
export default class ReadMore extends React.Component {
state = {
measured: false,
shouldShowReadMore: false,
showAllText: false
};
state = {...initialState};

resetState() {
this.setState({...initialState});
}

async componentDidMount() {
this._isMounted = true;
Expand All @@ -16,6 +21,22 @@ export default class ReadMore extends React.Component {
return;
}

this.compareTextHeightMeasurements()
}

async componentDidUpdate(prevProps) {
if (this.props.children !== prevProps.children) {
this.resetState();
await nextFrameAsync();
this.compareTextHeightMeasurements();
}
}

componentWillUnmount() {
this._isMounted = false;
}

async compareTextHeightMeasurements() {
// Get the height of the text with no restriction on number of lines
const fullHeight = await measureHeightAsync(this._text);
this.setState({ measured: true });
Expand All @@ -37,10 +58,6 @@ export default class ReadMore extends React.Component {
}
}

componentWillUnmount() {
this._isMounted = false;
}

render() {
let { measured, showAllText } = this.state;

Expand Down