React JS Tutorials for Beginners - 1 - Getting Started https://www.youtube.com/watch?v=-AbaV3nrw6E&list=PL6gx4Cwl9DGBuKtLgPR_zWYnrwv-JllpA Downloads: https://facebook.github.io/react/downloads.html Sample code:https://github.com/buckyroberts/React-Boilerplate browser.min.js: Convert jsx into javascript, jsx转换网站https://babeljs.io/repl/ React JS Tutorials for Beginners - 2 - Simple Demo https://www.youtube.com/watch?v=2NLgQMs2hOw&index=2&list=PL6gx4Cwl9DGBuKtLgPR_zWYnrwv-JllpA Sample Demo: 3. Components: 4. Props (Property) 5. Event Handling 6. State 7. Adding State to Components 8. Multiple Child Components (在一个容器内排序多个Components) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- constructor(){ super(); this.state={name: "Will"}; } this.setState({name:"Bob"}); changeTitle={ this.changeTitle.bind(this)} //changeTitle方法在当前js文件中需要用.bind(this) REACT JS TUTORIAL #6 - React Router & Intro to Single Page Apps with React JS https://www.youtube.com/watch?v=1iAG6h9ff5s&index=6&list=PLoYCgNOIyGABj2GQSlDRjgvXtqfDxKm5b Bootstrap模版: startbootstrap.com 安装react router的命令: npm install -S react-router Store类似于Angularjs中的Service. componentWillMount(){ //Component的构造函数 TodoStore.on("change",this.getTodos); } componentWillUnmount(){ //必须卸载这个方法,不然会导致内存泄露. TodoStore.removeListener("change",this.getTodos); } npm install -S flux Actions -> Dispatcher -> Stores -> Components -> Actions. import * as TodoAcions from "../action/TodoAcions"; //表示TodoActions中的所有方法. Ajax: axios("http://someurl.com/somedataendpoint").then((data)=>{ console.log("got the data!",data); }); Redux核心概念有三个:actions,store,reducers. Immutable JS - Redux Tutorial #2 - React.js Tutorial https://www.youtube.com/watch?v=9M-r8p9ey8U&list=PLoYCgNOIyGABj2GQSlDRjgvXtqfDxKm5b&index=16 var a={name:"Will", things:[0,1,2]} var b=Object.assign({},a,{name:"Fred"}) b.things=a.things.concat(3) state={...state,age:action.payload} Redux Middleware Tutorial - Redux Tutorial #5 https://www.youtube.com/watch?v=DJ8fR0mZM44&list=PLoYCgNOIyGABj2GQSlDRjgvXtqfDxKm5b&index=19 中间件: import {applyMiddleware,createStore} from "redux"; const logger=(store)=>(next)=>(action)=>{ console.log("action fired",action); next(action); } const store=createStore(reducer,1,middleware) Connecting React & Redux - Redux Tutorial #7 https://www.youtube.com/watch?v=nrg7zhgJd4w&index=21&list=PLoYCgNOIyGABj2GQSlDRjgvXtqfDxKm5b import {connect} from "react-redux" @connect((store)=>{ return{ user:store.user.user, userFetched:store.user.fetched, tweets:store.tweets.tweets, }; }) ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ReactJS / Redux Tutorial - #1 Introduction https://www.youtube.com/watch?v=qrsle5quS7A&list=PL55RiY5tL51rrC3sh8qLiYHqUV3twEYU_ npm install redux -save //安装redux js中的对象: const initialState = { result:1, lastValues:[] } state = { ...state, //ES6语法,包括state对象的所有属性 result: state.result + action.payload, lastValues: [...state.lastValues,action.payload] } ReactJS / Redux Tutorial - #5 Multiple Reducers https://www.youtube.com/watch?v=BVvBa18o8Es&list=PL55RiY5tL51rrC3sh8qLiYHqUV3twEYU_&index=5 import {createStore, combineReducers} from "redux" const store = createStore(combineReducers({mathReducer,userReducer})); ReactJS / Redux Tutorial - #6 Redux Middleware https://www.youtube.com/watch?v=AgO7YcJeBh4&index=6&list=PL55RiY5tL51rrC3sh8qLiYHqUV3twEYU_ import {createStore, combineReducers, applyMiddleware} from "redux" const myLogger = (store) => (next) => (action) => { console.log("Logged Action",action); next(action); } const store = createStore(combineReducers({mathReducer,userReducer}), {}, applyMiddleware(myLogger)); npm install redux-logger --save import logger from "redux-logger"; const store = createStore(combineReducers({mathReducer,userReducer}), {}, applyMiddleware(logger())); ReactJS / Redux Tutorial - #7 Connect ReactJS and Redux https://www.youtube.com/watch?v=tfuZ7uZmVyg&list=PL55RiY5tL51rrC3sh8qLiYHqUV3twEYU_&index=7 npm install react-redux --save import {connect} from "react-redux"; import {Provider} from "react-redux";const mapStateToProps = (state) => { return { user: state.userReducer, math: state.mathReducer } } const mapDispatchToProps = (dispatch) => { return { setName:(name)=>{ dispatch({ type:"SET_NAME", payload: name }); } } } export default connect(mapStateToProps,mapDispatchToProps)(app); this.props.setName("Anna")} /> this.props.user.name} /> ReactJS / Redux Tutorial - #8 Containers & Components (Smart & Dumb Components) https://www.youtube.com/watch?v=m2q3Dyr6To4&list=PL55RiY5tL51rrC3sh8qLiYHqUV3twEYU_&index=8 Dumb Components 只是方法不是类, 只需要return html不需要render. Smart Components放在containers文件夹下, Dumb Components放在components下. ReactJS / Redux Tutorial - #9 A better Project Structure https://www.youtube.com/watch?v=YmGm-qwbJdc&list=PL55RiY5tL51rrC3sh8qLiYHqUV3twEYU_&index=9 app actions, components, containers(app.js), reducers index.js store.js ReactJS / Redux Tutorial - #10 Async Actions https://www.youtube.com/watch?v=h892pHdLQtM&index=10&list=PL55RiY5tL51rrC3sh8qLiYHqUV3twEYU_ npm install redux-thunk --save import thunk from "redux-thunk"; applyMiddleware(logger(),thunk) npm install redux-promise-middleware --save import promise from "redux-promise-middleware"; applyMiddleware(logger(),thunk,promise()) return { type:"SET_NAME", payload:new Promise((resolve, reject) => { setTimeout(()=>{ resolve(name); },2000); }) } Reducer.js中 case "SET_NAME_FULFILLED" //因为这个promise中间件,需要加上_FULFILLED ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Learning React.js [1] - An Overview https://www.youtube.com/watch?v=vYldnghykaU&list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO when a state is changed, the component is re-rendered and markup will be updated. This makes React very dynamic. Lifecycle Methods: render - Renders a component. The only one that is required. getInitialState - You can set default values for your states. getDefaultProps - Set defaults for properties. componentWillMount - Invoked once on client & server before render. componentDidMount - Invoked after the first render. React Addons: Collection of modules that aid in developing React.js applications Animation Addons 2 Way Data Binding Addons - React Link Testing Addons Learning React.js [2] - Your First Component https://www.youtube.com/watch?v=mDz4HXZHo9g&index=3&list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO React网址: https://facebook.github.io/react/docs/getting-started.html Learning React.js [3] - Adding Properties https://www.youtube.com/watch?v=euSbXxCf88I&index=2&list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO propTypes:{ title:React.PropTypes.string.isRequired }, getDefaultProps: function(){ return { title: "111", text: "222", link:"333" } } Learning React.js [4] - Events https://www.youtube.com/watch?v=CVigtRUxj2I&list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO&index=4 this.OnClick.bind(this, 'Hello','Goodbye')}> //带参数的function onClick: function(msg,msg2){ alert(msg2); //弹出Goodbye } Learning React.js [5] - State and Nesting Components https://www.youtube.com/watch?v=_o9NTYfbkR0&index=6&list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO getInitialState: function(){ return { text: 'Hello World' } } { this.state.text}
this.changeText} value={ this.state.text} /> changeText: function(e){ this.setState({text: e.target.value}); //setState, e.target.value } Learning React.js [6] - Mapping Data https://www.youtube.com/watch?v=499IaPWLHKU&index=5&list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO React中的循环:{ this.props.todos.map(todo => { return
- {todo.name}
}) }Learning React.js [7] - Adding State Data Through a Form https://www.youtube.com/watch?v=yOu_PUAOtP0&index=7&list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO App component:
this.handleTodoAdd} /> handleTodoAdd: function(text){ var newTodo={ id: this.state.todos.length + 1, text: text } this.setState({todos: this.state.todos.concat(newTodo)}); //concat向数组里面添加对象 } TodoForm component: onSubmit:function(e){ //e参数, refs e.preventDefault(); var text = this.refs.text.value.trim(); if(!text){ alert('Please enter a todo'); return; } this.props.onTodoAdd(text); //调用传过来的props方法onTodoAdd() this.refs.text.value=''; } Learning React.js [8] - Deleting State Data https://www.youtube.com/watch?v=AUso8hw2-JQ&index=8&list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO this.state.todos} deleteTodo={ this.handleTodoDelete} /> //这里的handleTodoDelete不用指定参数 handleTodoDelete: function(todo){ var todos = this.state.todos; for(var i=0;i ){ if(todos[i].id==todo.id){ todos.splice(i,1); } } this.setState({todos: todos}); } this.onDelete.bind(this,todo)} > onDelete(todo){ this.props.deleteTodo(todo); } Learning React.js [9] - Updating State Data https://www.youtube.com/watch?v=WI8Z1RKzhMM&index=9&list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO this.state} onTodoAdd={ this.handleTodoAdd} /> //{...this.state}会把整个state传到控件TodoForm中,TodoForm控件中用{this.props.text}来获得state中的值. <TodoList //todos={this.state.todos} {...this.state} //{...this.state}会把整个state传到该控件中 editTodo={ this.handleTodoEdit} /> handleTodoEdit: function(todo){ this.setState({text:todo.text, isEdit: todo.id}); } this.editTodo.bind(this,todo)}> editTodo: function(todo){ this.props.editTodo(todo); } <TodoForm {...this.state} changeText={ this.handleChangeText} onTodoUpdate={ this.handleTodoUpdate} /> handleChangeText: function(text){ this.setState({text: text}); } handleTodoUpdate: function(todo){ var todos = this.state.todos; for(var i=0;i ){ if(todos[i].id==todo.id){ todos.splice(i,1); } } todos.push(todo); this.setState({todos: todos}); } TodoForm 组件中: this.onChange}> onChange: function(e){ this.props.changeText(e.target.value); //input没有onChange方法, 没有this.props.changeText的话input中的值输入会没有变化 } onSubmit方法中: if(this.props.isEdit){ var updateTodo = { id: this.props.isEdit, text:text } this.props.onTodoUpdate(updatedTodo); }else{ this.props.onTodoAdd(text); } Learning React.js [10] Persisting Data To Firebase https://www.youtube.com/watch?v=QY7Ibl37_08&list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO&index=10 页面代码中添加: componentWillMount: function(){ this.firebaseRef = new Firebase('https://todolistdev1.firebaseio.com/todos'); var that = this; this.firebaseRef.once("value", function(snapshot){ //Get todo list from database var todos = []; snapshot.forEach(function(data){ var todo = { id: data.val().id, text: data.val().text } todos.push(todo); that.setState({todos: todos}); }); }); } Add: this.firebaseRef.push(newTodo); ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- React & Express Starter Pack For Full Stack Development https://www.youtube.com/watch?v=v0t42xBIYIs 项目模版的使用: git clone https://... npm install npm run client-install npm run dev //会同时启动客户端和服务端,因为使用了模块concurrently. localhost:3000打开客户端, localhost:5000/api/customers打开服务端. 创建项目: mkdir reactexpress cd reactexpress npm init npm i express concurrently npm i nodemon --save-dev package.json文件修改: "scripts":{ "start":"node server.js", "server":"nodemon server.js" } touch server.js //创建文件 code server.js //用vs code打开文件 npm run server //启动服务端 打开另外一个命令窗口: npm i -g create-react-app //安装react的cli create-react-app client //创建react项目放在client下 打开client>package.json文件添加节点"proxy":"http://localhost:5000"指定服务端地址. npm start //启动react客户端 npm run dev同时启动客户端和服务端, 修改server端下面的package.json文件: "scripts":{ "client-install":"cd client && npm install", "start":"node server.js", "server":"nodemon server.js", "client":"npm start --prefix client", "dev":"concurrently \"npm run server\" \"npm run client\"" } -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- React JS Crash Course https://www.youtube.com/watch?v=A71aqufiNtQ 官网: https://facebook.github.io/rect npm install -g create-react-app create-react-app projectmanager //创建项目命令 npm start React使用的语法叫JSX. 生成GUID的插件: npm install --save uuid npm start import uuid from 'uuid'; uuid.v4(); //生成GUID 安装jquery: npm install jquery --save npm start import $ from 'jquery'; react里面不用name用ref? if(this.refs.title.value === '') child component: this.props.addProject(this.state.newProject); father component: this.handleAddProject.bind(this)} /> handleAddProject(project){ console.log(project); } handleDeleteProject(id){ let projects=this.state.projects; let index=projects.findIndex(x=>x.id===id); //findIndex找到要删除元素的序列号 projects.splice(index,1); this.setState({projects:projects}); } 类型验证: Projects.propTypes={ projects:React.PropTypes.array, onDelete:React.PropTypes.func } setState有返回函数: this.setState({todos:data},function(){ console.log(this.state); }); state在构造函数中初始化, 在componentWillMount中赋值. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The 2017 React Development Starter Guide https://www.youtube.com/watch?v=7ojad6QYuqI Add Todo: this.state={ todoTitle:'' } this.handleInputChange=this.handleInputChange.bind(this); //放在构造函数里面? handleInputChange(event){ const target = event.target; const target = event.target; const target = event.target; this.setState({ [name]:value }) } handleSubmit(event){ event.preventDefault(); this.props.onAddTodo(this.state); this.setState({ todoTitle:'' }); }
转载于:https://www.cnblogs.com/cw_volcano/p/8854770.html