diff --git a/snippets/LimitedWordTextArea.md b/snippets/LimitedWordTextArea.md deleted file mode 100644 index 662ec93dd..000000000 --- a/snippets/LimitedWordTextArea.md +++ /dev/null @@ -1,62 +0,0 @@ -### LimitedWordTextArea - -Renders a textarea component with a word limit. - -Use the value of the `value` prop to determine the initial `state.content` and `state.wordCount` and the value of the `limit` props to determine the value of `state.limit`. -Create a method, `handleChange`, which trims the `event.target.value` data if necessary and uses `Component.prototype.setState` to update `state.content` and `state.wordCount`, and bind it to the component's context. -In the`render()` method, use a`
` to wrap both the` -

{this.state.wordCount} / {this.props.limit}

-
- ); - } -} -``` - -```jsx -ReactDOM.render( - , - document.getElementById('root') -); -``` - -< !--tags: input,state,class -- > - -< !--expertise: 0 -- > - diff --git a/snippets/LimitedWordTextarea.md b/snippets/LimitedWordTextarea.md new file mode 100644 index 000000000..189b54ff1 --- /dev/null +++ b/snippets/LimitedWordTextarea.md @@ -0,0 +1,61 @@ +### LimitedWordTextarea + +Renders a textarea component with a word limit. + +Use the `React.useState()` hook to create the `content` and `wordCount` state variables and set their values to `value` and `0` respectively. +Create a method `setFormattedContent`, which uses `String.prototype.split(' ')` to turn the input into an array of words and check if the result of applying `Array.prototype.filter(Boolean)` has a `length` longer than `limit`. +If the afforementioned `length` exceeds the `limit`, trim the input, otherwise return the raw input, updating `content` and `wordCount` accordingly in both cases. +Use the `React.useEffect()` hook to call the `setFormattedContent` method on the value of the `content` state variable. +Use a`
` to wrap both the`