react 元件之間的引數傳遞
阿新 • • 發佈:2018-12-09
const MessageBox = React.createClass({ getInitialState:function() { return { title: ‘props屬性傳值’, messages: [‘messages1’,’messages2’,’messages3’,’messages4’] } }, render: function () { return (
我是子元件1
{this.state.title}
) } }); const SubMessageBox = React.createClass({ propTypes: { // 宣告資料型別 messages: React.PropTypes.array.isRequired }, getDefaultProps: function(){ return { messages: [‘若未傳message,會報未定義,故此這裡定義一個預設值,即使為空陣列也行,但一定要定義’] } }, render:function() { {/* let messages = []; this.props.messages.forEach(function(msg,index) { console.log(msg); messages.push(msg) }); console.log(this.props.messages)*/} return(我是子元件2
{this.props.messages}
) } }) const Footer = React.createClass({ render:function() { let messages = []; this.props.messages.forEach(function(msg,index) { msg += index messages.push(msg) }); return (我是子元件3
{messages}
) } }) const MessageBoxObj = React.render( , document.getElementById(‘app’), function() { console.log(‘渲染完成’); } );