第一个React项目及优化过程

功能需求

创建一个todolist,输入框输入新增的todo,回车追加到下面的todolist中。
点击todo后面的撤销 将此todo从todolist中删除。

第一版:

class ToDoList extends React.Component{
	constructor(props){
		super(props);
		this.state={
			"todoList": [
				"do the shopping",
				"clean the rooms"
			]
		};
	}
	
	keyDownHandle=(e)=>{
		const newTodoList = this.state.todoList;
		if(e.keyCode == 13){
			newTodoList.push(e.target.value);
			this.setState({"todoList": newTodoList});
		}
	}
	
	redoClick=(e)=>{
		const todoList = this.state.todoList;
		const delItem = e.target.dataset.item;
		todoList.splice(todoList.indexOf(delItem), 1);
		this.setState({"todoList": todoList});
	}
	
	render(){
		const todoList = this.state.todoList;
		const listItems = todoList.map((todo) =>
							<li key={todo}><span>{todo}<img src="redo.png" onClick={this.redoClick} data-item={todo}/></span></li> );
		return(
			<div>
				<input type="text" onKeyDown={this.keyDownHandle}/>
				<ul>{listItems}</ul>
			</div>
		);
	}
}

ReactDOM.render(
    <ToDoList />,
	document.getElementById("root")
)

第一版:
已实现:
1.追加新的todo
2.从列表中删除todo

未实现:
追加todo之后,input框中的内容没有清空。

第二版

1.this.state中追加inputValue键。
2.render的input value设置为this.state.inputValue,添加onChange事件handle changeHandle。
3.changeHandle中,将input的value更新到todoList的inputValue中。
4.keyDownHandle中,通过this.setState({"inputValue":""})清空input的内容。
class ToDoList extends React.Component{
	constructor(props){
		super(props);
		this.state={
			"todoList": [
				"do the shopping",
				"clean the rooms"
			],
			"inputValue": "order the newspaper"
		};
	}
	
	changeHandle=(e)=>{
		this.setState({"inputValue":e.target.value});
	}
	
	keyDownHandle=(e)=>{
		const newTodoList = this.state.todoList;
		if(e.keyCode == 13){
			newTodoList.push(e.target.value);
			this.setState({"todoList": newTodoList, "inputValue":""});
		}
	}
	
	redoClick=(e)=>{
		const todoList = this.state.todoList;
		const delItem = e.target.dataset.item; //删除img中的属性data-item={todo},改用this.state
		//const delItem = this.state.inputValue;
		todoList.splice(todoList.indexOf(delItem), 1);//改变React的state的数据时,拷贝出一个副本,修改副本。--》推荐
		this.setState({"todoList": todoList});
	}
	
	render(){
		const todoList = this.state.todoList;
		const listItems = todoList.map((todo) =>
							<li key={todo}><span>{todo}<img src="redo.png" data-item={todo} onClick={this.redoClick}/></span></li> );
		return(
			<div>
				<input type="text" onKeyDown={this.keyDownHandle} value={this.state.inputValue} onChange={this.changeHandle}/>
				<ul>{listItems}</ul>
			</div>
		);
	}
}

ReactDOM.render(
    <ToDoList />,
	document.getElementById("root")
)

React不操作Dom元素,改变数据Dom就会重新render。

第二版:
redoClick使用data-item反找index进行删除。不如直接将index传递给redoClick函数。

get到如何从元素中传递参数到函数中onClick={this.redoClick.bind(this,index)}
class ToDoList extends React.Component{
	constructor(props){
		super(props);
		this.state={
			"todoList": [
				"do the shopping",
				"clean the rooms"
			],
			"inputValue": "order the newspaper"
		};
	}
	
	changeHandle=(e)=>{
		this.setState({"inputValue":e.target.value});
	}
	
	keyDownHandle=(e)=>{
		const newTodoList = this.state.todoList;
		if(e.keyCode == 13){
			newTodoList.push(e.target.value);
			this.setState({"todoList": newTodoList, "inputValue":""});
		}
	}
	
	redoClick=(index)=>{
		const todoList = this.state.todoList;
		todoList.splice(index, 1);//改变React的state的数据时,拷贝出一个副本,修改副本。--》推荐
		this.setState({"todoList": todoList});
	}
	
	render(){
		const todoList = this.state.todoList;
		const listItems = todoList.map((todo, index) =>
							<li key={index}><span>{todo}<img src="redo.png" onClick={this.redoClick.bind(this,index)}/></span></li> );
		return(
			<div>
				<input type="text" onKeyDown={this.keyDownHandle} value={this.state.inputValue} onChange={this.changeHandle}/>
				<ul>{listItems}</ul>
			</div>
		);
	}
}

ReactDOM.render(
    <ToDoList />,
	document.getElementById("root")
)

第四版:组件拆分
将li封装为TodoItem组件。

父-》子组件间参数的传递:
1.父组件通过属性向子组件传递参数。
2.子组件中使用this.props.XXX接收。

子-》父组件通信:
子组件要调用父组件的方法。
class TodoItem extends React.Component{
	clickHandle=()=>{
		this.props.itemDeleteHandle();
	}
	
	render(){
		const index = this.props.index;
		const todo = this.props.todo;
		return(
			<li key={index}><span>{todo}<img src="redo.png" onClick={this.clickHandle}/></span></li>
		);
	}
}

class ToDoList extends React.Component{
	constructor(props){
		super(props);
		this.state={
			"todoList": [
				"do the shopping",
				"clean the rooms"
			],
			"inputValue": "order the newspaper"
		};
	}
	
	changeHandle=(e)=>{
		this.setState({"inputValue":e.target.value});
	}
	
	keyDownHandle=(e)=>{
		const newTodoList = this.state.todoList;
		if(e.keyCode == 13){
			newTodoList.push(e.target.value);
			this.setState({"todoList": newTodoList, "inputValue":""});
		}
	}
	
	redoClick=(index)=>{
		const todoList = this.state.todoList;
		todoList.splice(index, 1);//改变React的state的数据时,拷贝出一个副本,修改副本。--》推荐
		this.setState({"todoList": todoList});
	}
	
	render(){
		const todoList = this.state.todoList;
		const listItems = todoList.map((todo, index) =>
							<TodoItem todo={todo} index={index} itemDeleteHandle={this.redoClick.bind(this, index)}/> );
		return(
			<div>
				<input type="text" onKeyDown={this.keyDownHandle} value={this.state.inputValue} onChange={this.changeHandle}/>
				<ul>{listItems}</ul>
			</div>
		);
	}
}

ReactDOM.render(
    <ToDoList />,
	document.getElementById("root")
)

第五版:性能优化
1.bind去除
2.代码较多不易读时,抽出函数
3.式样美化
4.render只能识别一个JSX,但是有时式样布局的要求使得不能使用div包裹,可以改为使用React.Fragement

class TodoItem extends React.Component{
	clickHandle=()=>{
		this.props.itemDeleteHandle(this.props.index);
	}
	
	render(){
		const index = this.props.index;
		const todo = this.props.todo;
		return(
			<div key={index}><span>{todo}<img src="redo.png" onClick={this.clickHandle}/></span></div>
		);
	}
}

class ToDoList extends React.Component{
	constructor(props){
		super(props);
		this.state={
			"todoList": [
				"do the shopping",
				"clean the rooms"
			],
			"inputValue": "order the newspaper"
		};
	}
	
	changeHandle=(e)=>{
		this.setState({"inputValue":e.target.value});
	}
	
	keyDownHandle=(e)=>{
		const newTodoList = this.state.todoList;
		if(e.keyCode == 13){
			newTodoList.push(e.target.value);
			this.setState({"todoList": newTodoList, "inputValue":""});
		}
	}
	
	redoClick=(index)=>{
		const todoList = this.state.todoList;
		todoList.splice(index, 1);//改变React的state的数据时,拷贝出一个副本,修改副本。--》推荐
		this.setState({"todoList": todoList});
	}
	
	getItems=()=>{
		const todoList = this.state.todoList;
		return todoList.map((todo, index) =>
							<TodoItem 
							todo={todo} 
							index={index} 
							itemDeleteHandle={this.redoClick}/> 
						);
	}
	
	render(){
		return(
			<React.Fragment>
				<input className="input" type="text" onKeyDown={this.keyDownHandle} value={this.state.inputValue} onChange={this.changeHandle}/>
				<ul>{this.getItems()}</ul>
			</React.Fragment>
		);
	}
}

ReactDOM.render(
    <ToDoList />,
	document.getElementById("root")
)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: React电影项目实战是一个使用React框架和其他相关技术开发电影相关网站或应用程序的实践项目。在这个项目中,我们可以通过React的组件化开发方式和虚拟DOM的特性来构建动态的用户界面。 首先,我们需要设置React的开发环境。我们可以使用React脚手架工具来快速搭建一个基本的React应用程序,然后在此基础上进行开发。在这个项目中,我们还可以使用其他辅助工具和库,如Redux用于管理应用程序的状态、React Router用于实现页面路由、Axios用于发送HTTP请求等。 接下来,我们可以开始开发网站或应用程序的各个模块和功能。一个常见的功能是显示电影列表。我们可以使用Axios从后端API获取电影数据,并使用React组件来展示这些电影的标题、海报和描述等信息。我们还可以添加筛选、排序和搜索功能,以便用户可以根据自己的需求查找电影。 另一个常见的功能是电影详情页面。当用户点击某个电影时,我们可以展示该电影的详细信息,如演员阵容、上映日期和剧情简介。我们可以使用React Router实现页面之间的切换,并根据电影的ID从后端API获取相应的详细信息。 除了这些基本功能,我们还可以添加其他的功能和模块,如用户注册和登录、电影评分和评论、收藏和推荐功能等,以增加网站或应用程序的交互和用户体验。 在开发过程中,我们需要注意React的组件化开发原则和最佳实践。将页面拆分成多个可复用的组件,以提高代码的可维护性和可测试性。同时,在组件中使用React的生命周期方法和钩子函数来处理组件的状态和行为。 总而言之,React电影项目实战是一个实践React框架和其他相关技术的机会,通过开发电影相关的网站或应用程序,我们可以学习和应用React的各种特性和功能,提高自己的开发能力。 ### 回答2: React电影项目实战是指使用React框架来开发一个实际的电影网站或者电影相关的应用。这个项目可以包括电影列表展示、电影详情页面、搜索功能、用户评论和评分等功能。 在开始React电影项目实战之前,我们首先需要准备好开发环境,包括安装Node.js和创建React应用。然后,我们可以使用一些第三方库来辅助开发,如React Router用于实现页面路由、Axios用于发送HTTP请求、Redux用于状态管理等。 在项目中,我们可以使用电影相关的API来获取电影数据,比如通过豆瓣API获取电影列表、详情和评论等信息。在电影列表页面,我们可以使用React组件来展示电影海报、标题等信息,同时提供搜索功能给用户。当用户点击某个电影时,可以跳转到电影详情页面,展示电影的详细信息,并提供用户评论和评分的功能。 为了提升用户体验,我们可以使用React的组件化特性,将不同的功能拆分成多个小组件,然后将它们组合起来构建整个应用。同时,还可以使用React的虚拟DOM机制来优化性能,只对有变化的部分进行更新,提升应用的响应速度。 在完成项目开发后,我们可以使用一些工具和技术来打包和部署我们的React电影项目,比如使用Webpack来打包代码,并将项目部署到服务器上。 总之,React电影项目实战是一个很好的练习React开发技能和项目开发经验的机会。通过这个实战项目,我们可以学习如何使用React构建一个复杂的应用,掌握React的基本概念和开发流程,同时也可以提升自己的前端开发能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值