0

I am making a 'Markdown Previewer' using ReactJS. However, I am stuck at a code that seems to me just fine. The error is simply of an 'unexpected identifier ='. Can't seem to wrap my head around as to why it displays this error.

Following is my code snippet ..

class Markdown extends React.Component {
    static defaultProps = {
        text : 'This comes from defaultProps !'
    };

    static propTypes = {
        text : React.PropTypes.string.isRequired,
        onChange : React.PropTypes.func.isRequired;
    };

    constructor(props) {
        super(props);
        this.handleChange = this.handleChange.bind(this);
    }

    handleChange(e) {
        this.props.onChange(e.target.value);
    }

    render() {
        const style = {
            width : '100%',
            fontSize : '18px',
            border : '1px solid grey',
            padding : '20px'
        };

        return (
            <textarea style={style} rows='20' placeholder='// Enter text here ..' onChange='this.handleChange'>
                {this.props.text}
            </textarea>
        );
    }
}

Here is project code link .. https://codepen.io/iamrkcheers/pen/oeEyvo

The error occurs at line 67.

Any help is appreciated. Thank you.

0

3 Answers 3

3

My solution would be this, referring to the react documentation regarding type checking and creating defaultProps as a static function, to get them at everytime.

class Markdown extends React.Component {

    static defaultProps() { 
        return {
            text : 'This comes from defaultProps !'
        };
    }


    constructor(props) {
        super(props);
        this.handleChange = this.handleChange.bind(this);
    }

    handleChange(e) {
        this.props.onChange(e.target.value);
    }

    render() {
        const style = {
            width : '100%',
            fontSize : '18px',
            border : '1px solid grey',
            padding : '20px'
        };

        return (
            <textarea style={style} rows='20' placeholder='// Enter text here ..' onChange='this.handleChange'>
                {this.props.text}
            </textarea>
        );
    }
}

Markdown.propTypes = {
        text : React.PropTypes.string.isRequired,
        onChange : React.PropTypes.func.isRequired
};
Sign up to request clarification or add additional context in comments.

Comments

1

In JSX, you need to use double quotes for passing props to elements.

You're using single quotes, which won't work and causes the error.

Another mistake is enclosing your change handler in single quotes, in this case, you have to use curly braces, because it is actually a JavaScript argument you want React to execute, not a string.

return (
    <textarea style={style} rows="20" placeholder="// Enter text here .." onChange={this.handleChange}>
        {this.props.text}
    </textarea>
);

1 Comment

Yeah .. figured the 'quotes' instead of 'braces' part. Thanks.
0

Don't have any experience with React, but it does seem to me you need some sort of string quoting in your 'return' function. That for javascript would certainly throw the error you're seeing.

Comments

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.