创建一个简单的React demo

React中文文档

1.创建项目

npm init react-app my-app
cd my-app
npm start

npm init react-app my-app
运行这个命令会在当前目录中创建一个名为 my-app 的目录。
注意:项目路径不能有中文,不能有特殊符号!
cd my-app
表示进入项目
npm start
表示运行项目,完成后会自动打开浏览器看见项目页面

2. 新建Hello World 组件

将src下的所有文件删除,新建index.js

React团队目前推出最新的版本为18.0,在18.0版本中,React不再支持 ReactDOM.render,如果使用ReactDOM.render,控制台就会报错,但并不影响程序的正常运行。

// // 旧版本
// // 1.引入模块
// import React from 'react'
// import ReactDOM from 'react-dom'
// // 2.将组件挂载到指定元素上
// ReactDOM.render(
//    <h1> hello world!</h1>
//     , document.getElementById('root')
// )

// 新版本
import React from 'react'
import {createRoot} from 'react-dom/client'

const root = createRoot(document.getElementById('root'));
root.render(  <h1> hello world!</h1>);

打开刚刚浏览器页面,刷新发现刚刚的React图标页改成了hello world!页面

3. 组件化开发

src目录下新建App.js

//1、导入React核心模块
import React from 'react'

//2、定义组件类
class Hello extends React.Component{   //类
    render(){     //函数
        return (   //返回值 ,只能有一个根元素,但根元素内部可以有多个元素,类似于template
            <h1>
                Hello XiaoCheng !
            </h1>
        )
    }
}
// 导出组件
export  default Hello

注意 return 里面只能有一个根元素,但根元素内部可以有多个元素,类似于template。

在需要引入该组件的index.js中,导入,就可以直接使用这个组件了:


// 1.引入模块
import React from 'react'
import {createRoot} from 'react-dom/client'
import Hello from './App'//从具体文件引入组件
// 2.将组件挂载到指定元素上
const root = createRoot(document.getElementById('root'));
root.render(<Hello/>);



注意组件首字母必须大写!Hello 而不能写成hello。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个简单React Native Demo 代码,用于创建一个简单的登录界面: ```javascript import React, { useState } from 'react'; import { View, TextInput, TouchableOpacity, Text, StyleSheet } from 'react-native'; const App = () => { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const handleLogin = () => { // 在这里处理登录逻辑 console.log('登录:', username, password); }; return ( <View style={styles.container}> <TextInput style={styles.input} placeholder="用户名" value={username} onChangeText={(text) => setUsername(text)} /> <TextInput style={styles.input} placeholder="密码" secureTextEntry value={password} onChangeText={(text) => setPassword(text)} /> <TouchableOpacity style={styles.button} onPress={handleLogin}> <Text style={styles.buttonText}>登录</Text> </TouchableOpacity> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 16, }, input: { width: '100%', height: 40, borderWidth: 1, borderColor: '#ccc', borderRadius: 4, marginBottom: 10, paddingLeft: 10, }, button: { width: '100%', height: 40, backgroundColor: 'blue', justifyContent: 'center', alignItems: 'center', borderRadius: 4, }, buttonText: { color: '#fff', fontSize: 16, fontWeight: 'bold', }, }); export default App; ``` 这是一个简单的登录界面,包含了用户名和密码的输入框,以及一个登录按钮。当点击登录按钮时,会在控制台打印出输入的用户名和密码。你可以根据自己的需求来扩展和修改这段代码。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值