【发布时间】:2020-11-16 15:40:32
【问题描述】:
所以,我需要创建一个包含 2 个视图的 React Web 应用程序,其中一个是一个简单的数字猜谜游戏。我在我的电脑上安装了 npx 并创建了一个包含所有默认文件(app.js、index.js 等)的 react 应用程序。然而,我不知道如何在没有 html 文件的情况下进行编码。
我得到了一些这样的代码
index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width-device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link re="stylesheet" href="App.css">
<title>Document</title>
</head>
<body>
<div id="container">
<p>Guess a number between 1 - 100.</p>
<p id="outputtext">Enter a number below</p>
<input type= "text" id="userInput"><button id="btn">Enter</button>
</div>
<script src="App.js"></script>
</body>
</html>
style.css
background-color: #0E2431;
border: 5px solid #F5E4C3;
height: 275px;
width: 750px;
margin: auto;
text-align: center;
}
p{
color: white;
font-size: 24px;
}
#input {
width: 100px;
margin: 25px auto;
}
#btn {
margin: 25px auto auto 5px;
}```
game.js
``` let btn = document.getElementById('btn');
let output = document.getElementById('outputtext');
let number = [Math.floor(Math.random()*100)]
btn.addEventListener('click', function(){
let input = document.getElementById('userInput').value;
if (input == number){
output.innerHTML = 'You guessed right, your number was ${number}'
} else if (input < number){
output.innerHTML = "You guessed too low!"
};
if (input > number){
output.innerHTML = "You guessed too high!"
}
});```
But i don't know how i am able to recreate this in react, as there is no html file. Need help with this!
【问题讨论】:
-
嗨,里卡多斯!如果您之前从未接触过 React,我建议您在开始任何项目之前先阅读文档。 reactjs.org/docs/getting-started.html
标签: javascript html css reactjs