2

Basically im Creating A Markdown Previewer In Reactjs As Of Now Im Getting Error like Uncaught TypeError: value.replace is not a function and Had No Idea What Causing It Any Help Here if I Remove this.format from here dangerouslySetInnerHTML={this.format((this.rawMarkup()))} /> works fine but while typing on input box if i press enter it is not taking me to next line

var App =React.createClass({
    getInitialState:function(){
        return{
            value:"My Value"
        }
    },
    updateValue:function(modifiedValue){
        this.setState({
            value:modifiedValue
        })
    },

    format: function (value) {
        return { __html: value.replace(/\r?\n/g, '<br>') };
    },
    rawMarkup: function() {
        var rawMarkup = marked(this.state.value.toString(), {sanitize: true});
        return { __html: rawMarkup };
    },
    render:function(){
        return(
            <div className="inputBox container-fluid">
                <h1 className="text-center text-primary">Hello Coders !!!</h1>
                <div className="row">
                    <div className="col-md-6 col-md-offset-1">
                        <InputBox value={this.state.value} updateValue={this.updateValue}/>
                    </div>

                    <div
                        className="outputBox col-md-6 col-md-offset-1"
                        dangerouslySetInnerHTML={this.format((this.rawMarkup()))} />
                </div>
            </div>
        );
    }
});

var InputBox =React.createClass({
    update: function() {
        var modifiedValue = this.refs.initialValue.value;
        this.props.updateValue(modifiedValue);
    },

    render:function(){
        return(
            <textarea type="text" value={this.props.value} onChange={this.update} ref="initialValue">
                        </textarea>

        );
    }
});
ReactDOM.render(<App />,
    document.querySelector("#app")
);
<!DOCTYPE html>
<head>
    <meta charset="UTF-8" />
    <title>React Tutorial</title>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <style>
        .outputBox,textarea{
            width: 400px;
            border: 5px solid gray;
            margin: 0;
            height: 500px;
        }

    </style>
</head>
<div id="app"></div>
<script src="demo.js" type="text/babel"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.2/marked.min.js"></script>
</body>
</html>

1 Answer 1

1

As you are using markdown you don't need replace \n to <br>, you can just set breaks option to true

rawMarkup: function() {
  return { __html: marked(this.state.value, { sanitize: true, breaks: true }) };
},

Example

Sign up to request clarification or add additional context in comments.

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.