React学习(二)

1.React组件的两种创建方式

1.使用函数创建组件
2.使用类创建组件

1.1使用函数创建组件

·函数组件:使用JS的函数(或箭头函数)创建的组件
·函数名称必须以大写字母开头
·函数组件必须有返回值,表示该组件的结构
·如果返回值为null,表示不渲染任何内容
·渲染函数组件:用函数名作为组件标签名(组件标签可以是单标签也可以是双标签)

function Hello(){
   
	return (
		<div>这是一个函数组件!</div>
	)
}
ReactDOM.render(<Hello />,document.getElementById('root'))

ps:使用箭头函数创建组件

const Hello = () => <div>这是一个函数组件!</div>
ReactDOM.render(<Hello />,document.getElementById('root'))

1.2使用类创建组件(推荐)

·类组件:使用ES6的class创建的组件
·类名必须以大写字母开头
·类组件应该继承React.Component父类,从而可以使用父类中提供的方法或属性
·类组件必须提供render()方法
·render()方法必须有返回值,表示该组件的结构

class Hello extends React.Component{
   
	render(){
   
		return(
			<div>这是一个类组件!</div>
		)
	}
}
ReactDOM.render(<Hello />,document.getElementById('root'))

1.3抽离为独立JS文件

1.创建Hello.js
2.在Hello.js中导入React
3.创建组件(函数或类)
4.在Hello.js中导出该组件
5.在index.js中导入Hello组件
6.渲染组件

//Hello.js
import React from 'react'
class Hello extends React.Component{
   
	render(){
   
		return(
			<div>Hello Class Component!</div>
		)
	}
}
//导出Hello组件
export default Hello
//index.js
import Hello from './Hello'
//渲染导入的Hello组件
ReactDOM.render(<Hello />,'root')

2.React事件处理

2.1事件绑定

·React事件绑定语法与DOM事件语法相似
·语法:on+事件名称 = {事件处理程序},比如:onClick = {() = >{}}
·注意:React事件采用驼峰命名法,比如:onMouseEnter、onFocus

class App extends React.Component{
   
	handleClick(){
   
		console.log('单击事件触发了')
	}
	render(){
   
		return(
			<button onClkick = {
   this.handleClick}>点我</button>
		)
	}
}
ReactDOM.render(<App />,document.ge
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Learning React A hands-on guide to building web applications using React and Redux As far as new web frameworks and libraries go, React is quite the runaway success. It not only deals with the most common problems developers face when building complex apps, it throws in a few additional tricks that make building the visuals for such apps much, much easier. What React isn’t, though, is beginner-friendly and approachable. Until now. In Learning React , author Kirupa Chinnathambi brings his fresh, clear, and very personable writing style to help web developers new to React understand its fundamentals and how to use it to build really performant (and awesome) apps. The only book on the market that helps you get your first React app up and running in just minutes, Learning React is chock-full of colorful illustrations to help you visualize difficult concepts and practical step-by-step examples to show you how to apply what you learn. Build your first React app Create components to define parts of your UI Combine components into other components to build more complex UIs Use JSX to specify visuals without writing full-fledged JavaScript Deal with maintaining state Work with React’s way of styling content Make sense of the mysterious component lifecycle Build multi-page apps using routing and views Optimize your React workflow using tools such as Node, Babel, webpack, and others Use Redux to make managing your app data and state easy Contents at a Glance 1 Introducing React 2 Building Your First React App 3 Components in React 4 Styling in React 5 Creating Complex Components 6 Transferring Properties 7 Meet JSX... Again! 8 Dealing with State in React 9 Going from Data to UI in React 10 Events in React 11 The Component Lifecycle 12 Accessing DOM Elements in React 13 Setting Up Your React Dev Environment 14 Working with External Data in React 15 Building an Awesome Todo List App in React 16 Creating a Sliding Menu in React 17 Avoiding Unnecessary Renders in React 18 Creating a Single-Page App in React Using React Router 19 Introduction to Redux 20 Using Redux with React

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芝士奶盖四季春owo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值