gpt4 book ai didi

javascript - 简单组件未呈现 : React js

转载 作者:行者123 更新时间:2023-12-02 05:48:03 25 4
gpt4 key购买 nike

I am tring to do react using below code but I am not getting html element in the browser. There is no error in the console.

<!DOCTYPE html>
<html>
<head>
<title>React without npm</title>
<script src="https://unpkg.com/react@15/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
</head>
<body>
<div id="test"></div>
<script type="text/babel">

var reactTest = React.createClass({
render: function(){
return(
<h1>React Without NPM</h1>
);
}
});

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

有人可以帮忙吗。

最佳答案

如果 React Class namelowercase 开头字母然后class里面没有方法被调用,即没有任何渲染,并且您不会在浏览器控制台中收到任何错误消息,因为小写字母被视为 HTML元素,规则是所有React components必须以 upper case 开头字母,所以总是使用大写。

而不是 reactTest使用这个:ReactTest它会起作用。

根据 DOC :

用户定义的组件必须大写。

When an element type starts with a lowercase letter, it refers to a built-in component like <div> or <span> and results in a string 'div' or 'span' passed to React.createElement. Types that start with a capital letter like <Foo /> compile to React.createElement(Foo) and correspond to a component defined or imported in your JavaScript file.

We recommend naming components with a capital letter. If you do have a component that starts with a lowercase letter, assign it to a capitalized variable before using it in JSX.

检查工作代码:

<!DOCTYPE html>
<html>
<head>
<title>React without npm</title>
<script src="https://unpkg.com/react@15/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
</head>
<body>
<div id="test"></div>
<script type="text/babel">

var ReactTest = React.createClass({
render: function(){
return(
<h1>React Without NPM</h1>
);
}
});

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

关于javascript - 简单组件未呈现 : React js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42110144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com