React的起源和發展javascript
//建立組件 var Hello = React.createClass({ render:function () { //render函數和Vue組件裏的render徹底同樣,在vue組件中能夠不用編寫render函數,這個時候可使用template模板來編寫組件的虛擬dom結構,而後vue組件會自動講模板compile成虛擬dom結構放入到render中執行,可是react須要編寫render函數 return ( //jsx語法 <div>asdasd</div> ) } }) //利用ReactDOM對象的render方法將組件渲染到某個節點裏 ReactDOM.render(<Hello/>,document.getElementById("app"))
var world = React.createElement('h1',{className:'abc',id:'haha'},[ React.createElement('span',null,'Hello'), React.createElement('mark',null,'React') ]) //利用ReactDOM對象的render方法將組件渲染到某個節點裏 ReactDOM.render(world,document.getElementById("app1"))
<p style = { {color:'red',fontSize:2+'em'} }>Hello world</p>
<p className="bg-p" id="myp" style = { this.style }>Hello world</p>
handleClick:function () { alert(1) }, render:function () { return ( <div> <button onClick = {this.handleClick} className="click-btn">click</button> <button onDoubleClick = {this.handleClick} className="click-btn">click</button> </div> ) }
var Hello = React.createClass({ render(){ return ( <h1> Hello <World></World> </h1> ) } }) var World = React.createClass({ render(){ return ( <mark> World-<Person/> </mark> ) } }) //無狀態組件 var Person =function(){ return (<mark>lilei</mark>) } ReactDOM.render(<Hello/>,app)
var Gouzi = React.createClass({ render(){ console.log(this) return ( <div> <p>個人名字:{this.props.name}</p> <p>個人性別:{this.props.sex}</p> <p>個人年齡:{this.props.age}</p> <p>個人父親是:{this.props.father}</p> </div> ) } }) let info = { sex:'male', father:'狗爸' } ReactDOM.render(<Gouzi {...info} name={"大狗子"} age={26}/>,app)
//父組件的render函數 render(){ return ( <div> <p>父組件:</p> <hr/> <Son name={'大狗子'}/> <Son name={'二狗子'}/> </div> ) }
getDefaultProps:function () { console.log('getDefaultProps') return { name:'狗爸', sonname:'二狗子' } }, //render <p>我是{this.props.sonname}的父親-{this.props.name}</p>
getName(){ return this.props.name || '野狗子' }, render:function () { let {name} = this.props return ( <div> <p>我是子組件-{this.props.name || '野狗子'}</p> <p>我是子組件-{this.props.name?this.props.name:'野狗子'}</p> <p>我是子組件-{this.getName()}</p> <p>我是子組件-{(function (obj) { return obj.props.name || '野狗子' })(this)}</p> </div> ) }
//1. let doing = this.state.doing=='學習'+props.knowledge?'玩遊戲':'學習'+props.knowledge this.setState({doing}) //2. this.setState((prevState,props)=>{ return { doing:prevState.doing=='學習'+props.knowledge?'玩遊戲':'學習'+props.knowledge } })
<ul style={{display:isMenuShow?'block':'none'}}><li>國內新聞</li></ul> <ul className={isMenuShow?'show':'hide'}><li>國內新聞</li></ul>
{ isMenuShow?<ul><li>國內新聞</li></ul>:'' }
<ul ref='content'><li>國內新聞</li></ul> this.refs.content.style.display = this.state.isMenuShow?'block':'none'
//App <Dialog close={this.ToggleDialogShow} isShow={isDialogShow} > <ContentA/> <ContentA/> <ContentB/> </Dialog> //dialog <div style={{display:isShow?'block':'none'}} className="dialog"> <Button handler={this.props.close} text="關閉"/> {this.props.children}//這裏就是slot </div>
entry:'./src/app.js', output:{ path:path.join(__dirname,'build'), filename:'app.js' }
entry:['./src/app.js','./src/vendor.js'], output:{ path:path.join(__dirname,'build'), filename:'[name].js'//不肯定名字的時候,這裏會打包成main.js }
entry:{ app:'./src/app.js', vendor:'./src/vendor.js' }, output:{ path:path.join(__dirname,'build'), filename:'[name]_[hash].js' }
module.exports = (env)=>{ if(env=='production'){ return productionConfig } return developmentConfig }
npm i jade jade-loader --save-dev module:{ rules:[ { test:/\.jade$/, use:'jade-loader' } ] }, plugins:[ new HtmlWebpackPlugin({ // title:'webpack-config-demo', template:'./src/index.jade', filename:'index.html' }) ]
webpack-dev-server --hot true
devServer:{ port:9000, contentBase:'./build', historyApiFallback: true, open: true, proxy:{ } }
module:{ rules:[ {test:/\.css$/,use:'css-loader'} ], //before loaders:[ {test:/\.css$/,loader:'css-loader'} ], }
{ test:/\.(png|jpe?g|svg|gif)$/, // use:'url-loader?limit=1000&name=images/[hash:8].[name].[ext]' use:[ { loader:'url-loader', options:{ limit:1000, name:'/static/images/assets/[hash:8].[name].[ext]' } } ] }, { test:/\.html$/, use:'html-withimg-loader' }
{ test:/\.css$/, use:['style-loader','css-loader'] }
{ test:/\.scss$/, use:['style-loader','css-loader','sass-loader'] },
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin') ///loader { test:/\.css$/, use:ExtractTextWebpackPlugin.extract({ fallback: "style-loader", use: "css-loader" }) }, { test:/\.scss/, use:ExtractTextWebpackPlugin.extract({ fallback: "style-loader", use: ["css-loader","sass-loader"] }) } ///plugin new ExtractTextWebpackPlugin({ filename:'app.css', allChunks:true })
"babel": "^6.23.0", "babel-core": "^6.24.1", "babel-loader": "^7.0.0", "babel-preset-es2015": "^6.24.1", "babel-preset-react": "^6.24.1"
rules: { test:/\.js$/, exclude: /node_modules/, loader:'babel-loader', query: { presets: ['es2015','react'] } }
class App extends React.Component { }
class App extends Component { constructor(props){ super(props) this.state={ doing:'吃飯' } } }
App.defaultProps = { name:'App根組件' }
import PropTypes from 'prop-types'; App.propTypes = { name:PropTypes.string }
import React from 'react' const Button = (props)=>{ return <button onClick={props.handler}>我要花錢</button> } export default Button
// State 是一個對象 function reducer(state, action) { return Object.assign({}, state, { thingToChange }); // 或者 return { ...state, ...newState }; } // State 是一個數組 function reducer(state, action) { return [...state, newItem]; }
const routeConfig = [ { path: '/', component: App, indexRoute: { component: Home }, childRoutes: [ { path: 'home', component: Home }, { path: 'news', component: News, childRoutes: [ { path: 'inside', component: Inside }, { path: 'outside',component:Outside} ] }, { path: 'detail/:id', component: Detail }, {path:'*',component:Home} ] } ] ReactDOM.render( <Router routes={routeConfig} history={hashHistory}></Router> ,document.getElementById('app'))
import {createStore,applyMiddleware} from 'redux' import reducer from './reducer' import thunk from 'redux-thunk' const store = createStore(reducer,applyMiddleware(thunk)) export default store
const actions_thunk = { clearCart(){ //作異步操做以後生成action且dispatch return (dispatch)=>{ setTimeout(function(){ localStorage.removeItem('cars') let action = change_cars([]) dispatch(action) },500) } } }
import {bindActionCreators} from 'redux' import actions_thunk from '../../redux/ActionCreators/actions_thunk' export default connect(state=>state,dispatch=>{ return { actions_thunk:bindActionCreators(actions_thunk,dispatch) } })(ClearCar) /// button onClick={this.props.actions_thunk.clearCart}
<Son ref="son"></Son>
this.refs.son
<Son ref={(el)=>{this.son = el}}></Son> this.son
npm install redux-thunk -S //store import {createStore,applyMiddleware} from 'redux' import thunk from 'redux-thunk' import reducer from './reducer' const store = createStore(reducer,applyMiddleware(thunk)) //actionCreator const actionCreator = { handlerChange(){ return (dispatch)=>{ ... } } }