1

I have a TextArea object that will not allow new text to be entered. Initially I had tried this without the constructor, but would get areas when I would try to call my on change method because it was not bound to this. Adding the constructor to bind the onChange method disallows me from entering text.

        class TextAreaCounter extends React.Component{
            constructor(props) {
                super(props);
                this._textChange = this._textChange.bind(this);
            }
            getInitialState() {
                return {
                    text: this.props.text,
                };
            }
            _textChange(ev) {
                this.setState({
                    text: ev.target.value,
                });
            }
            render() {
                return React.DOM.div(null,
                    React.DOM.textarea({
                        value: this.props.text,
                        onChange: this._textChange,
                    }),
                    React.DOM.h3(null, this.props.text.length)
                );
            }
        }
        TextAreaCounter.PropTypes = {
            text: React.PropTypes.string,
        }
        TextAreaCounter.defaultProps = {
            text: '',
        }
        ReactDOM.render(
            React.createElement(TextAreaCounter, {
                text: "billy",
            }),
            document.getElementById("app")
        );

2 Answers 2

1

I found the answer!

First, I was using this.props instead of this.state in the render method. So, my onChange method was called but never actually updating.

Second, getIntialState is deprecated, so I update to a state object in the constructor.

       class TextAreaCounter extends React.Component{
            constructor(props) {
                super(props);
                this._textChange = this._textChange.bind(this);
                this.state = {
                    text: this.props.text,
                };
            }
            _textChange(ev) {
                this.setState({
                    text: ev.target.value,
                });
            }
            render() {
                return React.DOM.div(null,
                    React.DOM.textarea({
                        value: this.state.text,
                        onChange: this._textChange,
                    }),
                    React.DOM.h3(null, this.state.text.length)
                );
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

0

You should be passing this.state.text as the value to your textarea instead of this.props.text.

1 Comment

Correct! And I should set a state in the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.