diff --git a/src/App.js b/src/App.js
index 50a9bc6..053ef67 100644
--- a/src/App.js
+++ b/src/App.js
@@ -3,6 +3,8 @@ import React from "react";
import './App.css';
import InputComponent from "./components/InputComponent";
+import SonComponent from "./components/SonComponent";
+
function App() {
// vue3 组合式 API
// React 必须大写 Pascal 命名 组件
@@ -53,16 +55,16 @@ function App() {
console.log("setState 之外, 获取不到修改后的值 => ", this.state.a)
}
- addArr = ()=>{
- this.setState((state)=>{
- state.arr.push(state.arr[state.arr.length - 1 ] + 1)
+ addArr = () => {
+ this.setState((state) => {
+ state.arr.push(state.arr[state.arr.length - 1] + 1)
return {
...state,
// 解构赋值 使其地址改变 才能触发视图更新
// 不直接操作原对象, 要 先拷贝 再操作
arr: [...state.arr],
}
- }, ()=>{
+ }, () => {
console.log("state.arr => ", this.state.arr)
})
}
@@ -122,21 +124,21 @@ function App() {
show: true
}
- show = ()=>{
- this.setState((state)=>({
+ show = () => {
+ this.setState((state) => ({
...state,
show: true
}))
}
- hide = ()=>{
- this.setState((state)=>({
+ hide = () => {
+ this.setState((state) => ({
...state,
show: false
}))
}
- render(){
+ render() {
return
条件渲染
@@ -146,12 +148,13 @@ function App() {
-
}
}
@@ -198,6 +201,19 @@ function App() {
+
+
+ 父组件
+
+
);
diff --git a/src/components/SonComponent.js b/src/components/SonComponent.js
new file mode 100644
index 0000000..095f067
--- /dev/null
+++ b/src/components/SonComponent.js
@@ -0,0 +1,37 @@
+import React from "react";
+export default class SonComponent extends React.PureComponent {
+ state = {
+ msg: "SonComponent Msg"
+ }
+
+ // constructor(props) {
+ // super(props);
+ // console.log(props)
+ // }
+
+ render() {
+ return
+ 子组件
+
+
+ {this.state.msg}
+
+
+
+ {this.props.msg}
+
+
+ }
+}
+
+SonComponent.propTypes = {
+ msg: (props)=>{
+ if(typeof props.msg != "string"){
+ throw new Error("msg 必须为 string 类型")
+ }
+ }
+}
+
+SonComponent.defaultProps = {
+ msg: "defaultProps"
+}
diff --git a/src/components/SonComponent2.js b/src/components/SonComponent2.js
new file mode 100644
index 0000000..e25b283
--- /dev/null
+++ b/src/components/SonComponent2.js
@@ -0,0 +1,13 @@
+import React, {useState} from "react";
+export default function SonComponent2(props){
+ const [state] = useState({
+ msg: ""
+ })
+
+
+ return
+ 子组件
+ {state.msg}
+
+
+}