Skip to content

Commit 40ae7f4

Browse files
committed
feat: test 2 type of Comp
1 parent ce763e4 commit 40ae7f4

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

React/sandboxs/src/index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,24 @@ import React from 'react'; // 虽然也许可能没有使用,如果删了就
22
import ReactDOM from 'react-dom';
33

44
// import './test/test1'
5-
import './test/toggleImg'
5+
// import './test/toggleImg'
6+
7+
import FuncComp from './test/Comp/func'
8+
import ClassComp from './test/Comp/class';
9+
const el = (<FuncComp str="ceilf6" num={7}></FuncComp>)
10+
console.log(el)
11+
// 组件生成的仍然是React元素,但是 type: ƒ Comp()
12+
// 如果首字母不大写的话就是普通元素
13+
const el2 = (<ClassComp str="ceilf6" num={7}></ClassComp>) // type: class ClassComp
14+
console.log(el2)
15+
const allEl = (
16+
<>
17+
{el}
18+
{el2}
19+
{<FuncComp useless={false} useful
20+
obj={{ name: "ceilf6", age: 20, ui: (<div>nihao</div>) }}
21+
>
22+
</FuncComp >}
23+
</>
24+
)
25+
ReactDOM.render(allEl, document.getElementById('root'))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react'
2+
3+
export default class ClassComp extends React.Component {
4+
constructor(props) {
5+
super(props); // this.props = props
6+
console.log("=== props", props)
7+
}
8+
9+
render() {
10+
return (
11+
<h1>nihao2 {this.props.num}</h1>
12+
)
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// imr
2+
import React from 'react'
3+
4+
export default function FuncComp(props) {
5+
console.log("=== props",props)
6+
/**
7+
* 危险!!否则排查很麻烦
8+
*/
9+
// if(props?.obj?.name) props.obj.name = "250"
10+
// props.obj = {name: "250"}
11+
// error: readonly
12+
return (
13+
<>
14+
<h1>nihao {props.str} {props.num}</h1>
15+
<h1>objName: {props?.obj?.name}</h1>
16+
<h1>objUI: {props?.obj?.ui}</h1>
17+
</>
18+
)
19+
}

0 commit comments

Comments
 (0)