Skip to content

Commit

Permalink
Fixed Text crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
sainthkh committed Oct 16, 2019
1 parent 463f3c2 commit fbb1cbe
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/component/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Text extends Component {
constructor(props) {
super(props);
this.state = {
wordsByLines: []
wordsByLines: this.getWordsByLines(props, true),
};
}

Expand All @@ -68,6 +68,12 @@ class Text extends Component {
}

updateWordsByLines(props, needCalculate) {
this.setState({
wordsByLines: this.getWordsByLines(props, needCalculate),
});
}

getWordsByLines(props, needCalculate) {
// Only perform calculations if using features that require them (multiline, scaleToFit)
if ((props.width || props.scaleToFit) && !isSsr()) {
if (needCalculate) {
Expand All @@ -79,28 +85,31 @@ class Text extends Component {
this.wordsWithComputedWidth = wordsWithComputedWidth;
this.spaceWidth = spaceWidth;
} else {
this.updateWordsWithoutCalculate(props);

return;
return this.getWordsWithoutCalculate(props);
}
}

const wordsByLines = this.calculateWordsByLines(
return this.calculateWordsByLines(
this.wordsWithComputedWidth,
this.spaceWidth,
props.width
);
this.setState({ wordsByLines });
} else {
this.updateWordsWithoutCalculate(props);
}
return this.getWordsWithoutCalculate(props);
}

updateWordsWithoutCalculate(props) {
const words = !_.isNil(props.children) ? props.children.toString().split(BREAKING_SPACES) : [];
this.setState({ wordsByLines: [{ words }] });
}

getWordsWithoutCalculate = (props) => {
const words = !_.isNil(props.children) ?
props.children.toString().split(BREAKING_SPACES) :
[];
return [{ words }];
}

calculateWordsByLines(wordsWithComputedWidth, spaceWidth, lineWidth) {
const { scaleToFit } = this.props;
return (wordsWithComputedWidth || []).reduce((result, { word, width }) => {
Expand Down

0 comments on commit fbb1cbe

Please sign in to comment.