如何修复初学者的ReactJS错误? [英] How to fix the beginner's ReactJS error?

查看:61
本文介绍了如何修复初学者的ReactJS错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ReactJS的初学者,正在尝试运行以下代码:

I'm beginner in ReactJS and am trying to run the following code:

@{
    ViewBag.Title = "Index";
}

<h2>Hello World-React JS</h2>

<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>

<script type="text/javascript">
    var HelloWorldComponent = React.createClass({
        getInitialState: function () {
            return {
                serverMessage: ''
            };
        },
        componentDidMount: function () {
            $.get('/Home/getmessage', function (result) {
                if (this.isMounted) {
                    this.setState({
                        serverMessage: result
                    });
                }
            }.bind(this));
        },
        render: function () {
            return ("<h1>{this.state.serverMessage}</h1>");
        }
    });
    ReactDOM.render(<HelloWorldComponent />, document.getElementById("helloworldcontainer"));
</script>

<div id="helloworldcontainer"></div>

如您所见,这是VS2017 MVC简单测试应用程序.

As you can see this is VS2017 MVC simple test application.

实际上,在VS编辑器中,下面的屏幕快照显示了一些抱怨:

Actually, in VS editor I have some complaints that is shown on the screen shot bellow:

如何修复并运行它?

推荐答案

将脚本类型更改为<script type="text/babel">

示例代码:

<html>

<head>
    <title>demo</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
</head>
<body>
    <div id="container"/></div>

    <script type="text/babel">
        class MessageBox extends React.Component {
            constructor(props){
            super(props);
            }
            render() {
                return (
                    <div>
                        <div className={`messageBox ${this.props.type} || hidden`}>
                            {this.props.message}
                        </div>
                    </div>
                );
            }
        } 

    class NameInput extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                message: {
                type: "success",
                body: "Now my message is in NameInput's state"
                }
            }
            this.buttonClicked = this.buttonClicked.bind(this);
        }

        buttonClicked(evt) {
            alert("hi");
        }

        render() {

            let msg = this.state.message;

            return (
                <div>
                <label>Name: <input type="text" /></label>
                <button onClick={this.buttonClicked}>Click me!</button>

                <MessageBox type={msg.type} message={msg.body}/>
                </div>
            )
            }
    }

    ReactDOM.render(
        <NameInput />,
        document.getElementById('container')
    );
</script>
</body>
</html>

希望这会有所帮助.

只需检查react如何工作,就可以了.为了将来的开发,请使用babel-cli并尝试在单独的文件中编写组件.

as @Adeel Imran suggests, For just checking how react works, this is fine. For future development use babel-cli and try to write a component in a separate file.

这篇关于如何修复初学者的ReactJS错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆