一、组件实例三大核心属性之一:state
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2_state的简写方式</title>
</head>
<body>
<div id="test"></div>
<script type="text/javascript" src="../js/react.development.js"></script>
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/babel">
class Weather extends React.Component {
state = {isHot:true,wind:'微风'};
render() {
const {isHot,wind} = this.state;
return <h1 onClick={this.changeWeather}>今天天气很{isHot ? '炎热' : '凉爽'},{wind}</h1>
}
changeWeather = () => {
const {isHot} = this.state;
this.setState({
isHot: !isHot,
})
}
}
ReactDOM.render(<Weather/>,document.getElementById('test'))
</script>
</body>
</html>
二、组件实例三大核心属性之二:props
1、类式组件 使用props
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3_props的简写方式</title>
</head>
<body>
<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>
<script type="text/javascript" src="../js/react.development.js"></script>
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/javascript" src="../js/prop-types.js"></script>
<script type="text/babel">
class Person extends React.Component {
static propTypes = {
name: PropTypes.string.isRequired,
sex: PropTypes.string,
age: PropTypes.number,
speak:PropTypes.func,
}
static defaultProps = {
sex: '男',
age: 18,
}
render() {
console.log(this);
console.log(this.props);
const {name,age,sex} = this.props;
return (
<ul>
<li>姓名:{name}</li>
<li>性别:{sex}</li>
<li>年龄:{age+1}</li>
</ul>
)
}
}
ReactDOM.render(<Person name="tom" sex="女" speak={speak}/>,document.getElementById('test1'));
ReactDOM.render(<Person name="jerry" age={19} />,document.getElementById('test2'));
const p = {name:'老刘',age:18,sex:'女'}
console.log(...p);
ReactDOM.render(<Person {...p}/>,document.getElementById('test3'));
function speak() {
console.log('我说话了');
}
</script>
</body>
</html>
2、函数式组件 使用props
【组件实例三大核心属性:state、props、refs中,函数式组件只能使用props】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>4_函数式组件使用props</title>
</head>
<body>
<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>
<script type="text/javascript" src="../js/react.development.js"></script>
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/javascript" src="../js/prop-types.js"></script>
<script type="text/babel">
function Person(props) {
const {name,sex,age} = props;
console.log(props)
return (
<ul>
<li>姓名:{name}</li>
<li>性别:{sex}</li>
<li>年龄:{age}</li>
</ul>
)
}
Person.propTypes = {
name: PropTypes.string.isRequired,
sex: PropTypes.string,
age: PropTypes.number,
}
Person.defaultProps = {
sex: '男',
age: 18,
}
ReactDOM.render(<Person name="jerry"/>,document.getElementById('test1'));
</script>
</body>
</html>
三、组件实例三大核心属性之三:refs
1、字符串形式的ref 【过时】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>1_字符串形式的ref(过时)</title>
<style>
</style>
</head>
<body>
<div id="test"></div>
<script type="text/javascript" src="../js/react.development.js"></script>
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/babel">
class Demo extends React.Component {
showData = () => {
console.log(this);
const {input1} = this.refs;
alert(input1.value);
}
showData2 = () => {
console.log(this);
const {input2} = this.refs;
alert(input2.value);
}
render() {
return (
<div>
<input ref="input1" type="text" placeholder="点击按钮提示数据" />
<button onClick={this.showData}>点我提示左侧的数据</button>
<input ref="input2" onBlur={this.showData2} ref="input2" type="text" placeholder="失去焦点提示数据"/>
</div>
)
}
}
ReactDOM.render(<Demo/>,document.getElementById('test'));
</script>
</body>
</html>
2、回调函数形式的ref
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2_回调函数形式的ref</title>
<style>
</style>
</head>
<body>
<div id="test"></div>
<script type="text/javascript" src="../js/react.development.js"></script>
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/babel">
class Demo extends React.Component {
showData = () => {
console.log(this);
const {input1} = this;
alert(input1.value);
}
showData2 = () => {
console.log(this);
const {input2} = this;
alert(input2.value);
}
render() {
return (
<div>
<input ref={currentNode => this.input1 = currentNode} type="text" placeholder="点击按钮提示数据" />
<button onClick={this.showData}>点我提示左侧的数据</button>
<input ref={currentNode => this.input2 = currentNode} onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/>
</div>
)
}
}
ReactDOM.render(<Demo/>,document.getElementById('test'));
</script>
</body>
</html>
3、createRef 的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>4_createRef的使用</title>
</head>
<body>
<div id="test"></div>
<script type="text/javascript" src="../js/react.development.js"></script>
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/babel">
class Demo extends React.Component {
myRef = React.createRef();
myRef2 = React.createRef();
showData = () => {
console.log(this.myRef.current);
alert(this.myRef.current.value);
}
showData2 = () => {
console.log(this.myRef2.current);
alert(this.myRef2.current.value);
}
render() {
return (
<div>
<input ref={this.myRef} type="text" placeholder="点击按钮提示数据" />
<button onClick={this.showData}>点我提示左侧的数据</button>
<input ref={this.myRef2} onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/>
</div>
)
}
}
ReactDOM.render(<Demo/>,document.getElementById('test'));
</script>
</body>
</html>
These are bilibili尚硅谷React学习视频的 学习笔记~
|