7. props 属性
This commit is contained in:
parent
2773535e9d
commit
c70b581393
18
src/App.js
18
src/App.js
@ -3,6 +3,8 @@ import React from "react";
|
|||||||
import './App.css';
|
import './App.css';
|
||||||
|
|
||||||
import InputComponent from "./components/InputComponent";
|
import InputComponent from "./components/InputComponent";
|
||||||
|
import SonComponent from "./components/SonComponent";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
// vue3 组合式 API
|
// vue3 组合式 API
|
||||||
// React 必须大写 Pascal 命名 组件
|
// React 必须大写 Pascal 命名 组件
|
||||||
@ -151,7 +153,8 @@ function App() {
|
|||||||
...state,
|
...state,
|
||||||
show: !state.show
|
show: !state.show
|
||||||
}))
|
}))
|
||||||
}}>切换</button>
|
}}>切换
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -198,6 +201,19 @@ function App() {
|
|||||||
<IfComponent/>
|
<IfComponent/>
|
||||||
|
|
||||||
<InputComponent/>
|
<InputComponent/>
|
||||||
|
|
||||||
|
<div style={{
|
||||||
|
border: '1px solid',
|
||||||
|
width: '80%',
|
||||||
|
overflow: 'auto',
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'column',
|
||||||
|
alignItems: 'center',
|
||||||
|
justifyContent: 'center'
|
||||||
|
}}>
|
||||||
|
父组件
|
||||||
|
<SonComponent a={1} b={2} msg={"App Msg"}/>
|
||||||
|
</div>
|
||||||
</header>
|
</header>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
37
src/components/SonComponent.js
Normal file
37
src/components/SonComponent.js
Normal file
@ -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 <div style={{border: '1px solid', width: '80%', overflow: 'auto'}}>
|
||||||
|
子组件
|
||||||
|
|
||||||
|
<div>
|
||||||
|
{this.state.msg}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
{this.props.msg}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SonComponent.propTypes = {
|
||||||
|
msg: (props)=>{
|
||||||
|
if(typeof props.msg != "string"){
|
||||||
|
throw new Error("msg 必须为 string 类型")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SonComponent.defaultProps = {
|
||||||
|
msg: "defaultProps"
|
||||||
|
}
|
13
src/components/SonComponent2.js
Normal file
13
src/components/SonComponent2.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import React, {useState} from "react";
|
||||||
|
export default function SonComponent2(props){
|
||||||
|
const [state] = useState({
|
||||||
|
msg: ""
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
return <div style={{border: '1px solid', width: '80%', overflow: 'auto'}}>
|
||||||
|
子组件
|
||||||
|
{state.msg}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user