一、安装流程
npm i create-react-app -g
create-react-app myapp
cd myapp
npm run start
二、修改端口号
//在package.json修改start
"start": "set PORT=9000 && react-scripts start"
三、谷歌调用工具
https://pan.baidu.com/s/1eSZsXDC
四、组件&&props
//工厂模式
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
//ES6模式
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
//props
<Welcome name={ world } />
五、State & 生命周期
//state(状态)
class Welcome extends React.Component {
//初始化state
state = {
name : 'hello world'
}
//修改和更新state
nameClick = () => {
this.setState({
name : 'my name is dyk'
})
//this.props 和 this.state 可能会异步更新
this.setState((state, props) => ({
name : state.name + props.name
}));
}
render() {
//取出状态
const { name } = this.state;
return <h1 onClick={this.nameClick}>{name}</h1>;
}
}
//生命周期
//进入组件页面初始化
constructor(props) {
super(props);
this.state = {date: new Date()};
}
//组件渲染完成(挂载)
componentDidMount() {
/* 可以进行ajax操作,比如设置定时器 */
}
//组件被删除的时候(卸载)
componentWillUnmount() {
/* 可以结束执行一些方法,比如清除定时器 */
}
六、事件处理
//写法一:
class LoggingButton extends React.Component {
// 此语法确保 `handleClick` 内的 `this` 已被绑定。
// 注意: 这是 *实验性* 语法。
handleClick = () => {
console.log('this is:', this);
}
render() {
return (
<button onClick={this.handleClick}>
Click me
</button>
);
}
}
//写法二:
class LoggingButton extends React.Component {
handleClick() {
console.log('this is:', this);
}
render() {
// 此语法确保 `handleClick` 内的 `this` 已被绑定。
return (
<button onClick={(e) => this.handleClick(e)}>
Click me
</button>
);
}
}
//在循环中,通常我们会为事件处理函数传递额外的参数
<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
七、条件渲染
//三目运算符
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
<div>
The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in.
</div>
);
}
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
<div>
{isLoggedIn ? (
<LogoutButton onClick={this.handleLogoutClick} />
) : (
<LoginButton onClick={this.handleLoginClick} />
)}
</div>
);
}
//阻止组件渲染
function WarningBanner(props) {
if (!props.warn) {
return null;
}
return (
<div className="warning">
Warning!
</div>
);
}
class Page extends React.Component {
constructor(props) {
super(props);
this.state = {showWarning: true};
this.handleToggleClick = this.handleToggleClick.bind(this);
}
handleToggleClick() {
this.setState(state => ({
showWarning: !state.showWarning
}));
}
render() {
return (
<div>
<WarningBanner warn={this.state.showWarning} />
<button onClick={this.handleToggleClick}>
{this.state.showWarning ? 'Hide' : 'Show'}
</button>
</div>
);
}
}
ReactDOM.render(
<Page />,
document.getElementById('root')
);
八、表单
//受控组件(推荐)
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value.trim()});
}
handleSubmit(event) {
alert('提交的名字: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
名字:
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="提交" />
</form>
);
}
}
//非受控组件ref
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
alert('提交的名字: ' + this.inputContent .value.trim());
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
名字:
<input type="text" ref={ input => this.inputContent = input } />
</label>
<input type="submit" value="提交" />
</form>
);
}
}
//处理多个输入
当需要处理多个 input 元素时,我们可以给每个元素添加 name 属性,并让处理函数根据 event.target.name 的值选择要执行的操作。
class Reservation extends React.Component {
constructor(props) {
super(props);
this.state = {
isGoing: true,
numberOfGuests: 2
};
this.handleInputChange = this.handleInputChange.bind(this);
}
handleInputChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}
render() {
return (
<form>
<label>
参与:
<input
name="isGoing"
type="checkbox"
checked={this.state.isGoing}
onChange={this.handleInputChange} />
</label>
<br />
<label>
来宾人数:
<input
name="numberOfGuests"
type="number"
value={this.state.numberOfGuests}
onChange={this.handleInputChange} />
</label>
</form>
);
}
}
网友评论