可以在命令行中使用 npm run 的任务:
import React from 'react'
import ReactDOM from 'react-dom'
ReactDOM.render(
<h1>Hello Reacth1>,
document.getElementById('root')
)
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<h1>Hello, world!h1>);
<div>Hello { 'world' }</div>
注:使用 { expression } 来嵌入 JS 表达式内容,注意:在 {} 内的 HTML 表达式内容会被转义处理,以避免出现 XSS 攻击。
<div dangerouslySetInnerHTML={{__html: this.state.html}}></div>
function MyComponent(props) {
return (
<div>JSX 表示的 React 元素</div>
)
}
class MyComponent extends React.Component {
// constructor() {} // 可以省略
render() {
// this.props 获取组件接收到的属性
return (
<div>JSX 表示的 React 元素</div>
)
}
}
注意:
import PropTypes from 'prop-types'
class MyComponent extends React.Component {
static propTypes = {
prop1: PropTypes.number, // prop1式数字
prop2: PropTypes.array.isRequired, // prop2式数组并且是必填的
}
}
class MyComponent extends React.Component {
static defaultProps = {
prop1: 1,
prop2: 'abc',
prop3: [],
}
}
绑定DOM元素,能够在方法中调用到
import React, {Component} from 'react';
import Children1 from './ components/Children1'
class App extends Component {
myRef=React.createRef()
methods=()=>{
console.log(this.myRef.current)
// 指向div本身
}
render() {
return (
<>
<div ref={this.myRef} onClick={this.methods}>
hello react
</div>
<Children1/>
</>
);
}
}
export default App;
注:调用 Ref 对象的 current 属性获取关联的数据
import React, {Component} from 'react';
import Children1 from './ components/Children1'
class App extends Component {
state={
person:{
name:'jack',
age: '20',
}
}
render() {
return (
<>
<div>
hello react
</div>
<Children1 info={this.state.person}/> // 在这里通过属性的方式传值给子组件
</>
);
}
}
export default App;
import React, {Component} from 'react';
import PropTypes from 'prop-types'
class Children1 extends Component {
static propTypes={
info:PropTypes.object.isRequired
}
show=()=>{
console.log(this.props)
// 控制台打印{info: {…}}
}
render() {
return (
<div>
<div onClick={this.show()}>hello children</div>
<div>{this.props.info.name}</div>
</div>
);
}
}
export default Children1;
import {createContext} from 'react';
// 创建createContext对象
const TestContext=createContext()
// 解构要用到的组件
const {
Provider, // 生产组件
Consumer, // 消费组件
} = TestContext
export {
Provider,
Consumer,
TestContext,
}
import React, {Component} from 'react';
import Children1 from './ components/Children1'
import {Provider} from "./utils/with-context";
class App extends Component {
state={
person:{
name:'jack',
age: '20',
}
}
myRef=React.createRef()
methods=()=>{
console.log(this.myRef.current)
}
render() {
return (
<Provider value={{
person:this.state.person
}}>
<>
<div ref={this.myRef} onClick={this.methods}>
hello react
</div>
<Children1/>
</>
</Provider>
);
}
}
export default App;
import React, {Component} from 'react';
import {Consumer} from "../utils/with-context";
class Children1 extends Component {
render() {
return (
<Consumer>
{
value=>{
const {person} = value
return (
<div>
<div>hello children</div>
<div>{person.name}</div>
</div>
);
}
}
</Consumer>
)
}
}
export default Children1;
import React, {Component} from 'react';
import {Consumer, TestContext} from "../utils/with-context";
class Children1 extends Component {
handelContext=()=>{
console.log(this.context) //{person: {…}}
}
render() {
TestContext.displayName='rose'
return (
<Consumer>
{
value=>{
const {person} = value
return (
<div>
<div onClick={this.handelContext}>hello children</div>
<div>{person.name}</div> // jack
<div>{TestContext.displayName}</div> // rose
</div>
);
}
}
</Consumer>
)
}
}
Children1.contextType=TestContext // 定义类的静态属性,这样类中就能访问context
export default Children1;