ReactJS两个组件通信我刚开始使用ReactJS,在我遇到的一个问题上有点卡住了。我的应用程序本质上是一个带有过滤器和按钮来更改布局的列表。目前,我正在使用三个组件:<list />, < Filters />和<TopBar />,显然,当我在< Filters />我想在<list />更新我的观点。我如何使这3个组件相互交互,或者是否需要某种全局数据模型来对其进行更改?
3 回答
扬帆大鱼
TA贡献1799条经验 获得超9个赞
dates.js
/** @jsx React.DOM */ var monthsLength = [0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var MONTHS_ARR = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]; var DayNumber = React.createClass({ render: function() { return ( <option value={this.props.dayNum}>{this.props.dayNum}</option> ); } }); var DaysList = React.createClass({ getInitialState: function() { return {numOfDays: 30}; }, handleMonthUpdate: function(newMonthix) { this.state.numOfDays = monthsLength[newMonthix]; console.log("Setting days to " + monthsLength[newMonthix] + " month = " + newMonthix); this.forceUpdate(); }, handleDaySelection: function(evt) { this.props.dateHandler(evt.target.value); }, componentDidMount: function() { this.props.readyCallback(this.handleMonthUpdate) }, render: function() { var dayNodes = []; for (i = 1; i <= this.state.numOfDays; i++) { dayNodes = dayNodes.concat([<DayNumber dayNum={i} />]); } return ( <select id={this.props.id} onChange = {this.handleDaySelection}> <option value="" disabled defaultValue>Day</option> {dayNodes} </select> ); } }); var Month = React.createClass({ render: function() { return ( <option value={this.props.monthIx}>{this.props.month}</option> ); } }); var MonthsList = React.createClass({ handleUpdate: function(evt) { console.log("Local handler:" + this.props.id + " VAL= " + evt.target.value); this.props.dateHandler(evt.target.value); return false; }, render: function() { var monthIx = 0; var monthNodes = this.props.data.map(function (month) { monthIx++; return ( <Month month={month} monthIx={monthIx} /> ); }); return ( <select id = {this.props.id} onChange = {this.handleUpdate}> <option value="" disabled defaultValue>Month</option> {monthNodes} </select> ); } }); var LeftPanel = React.createClass({ dayRefresh: function(newMonth) { // Nothing - will be replaced }, daysReady: function(refreshCallback) { console.log("Regisering days list"); this.dayRefresh = refreshCallback; }, handleMonthChange: function(monthIx) { console.log("New month"); this.dayRefresh(monthIx); }, handleDayChange: function(dayIx) { console.log("New DAY: " + dayIx); }, render: function() { return( <div id="orderDetails"> <DaysList id="dayPicker" dateHandler={this.handleDayChange} readyCallback = {this.daysReady} /> <MonthsList data={MONTHS_ARR} id="monthPicker" dateHandler={this.handleMonthChange} /> </div> ); } }); React.renderComponent( <LeftPanel />, document.getElementById('leftPanel') );
<!DOCTYPE html><html><head> <title>Dates</title> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script> <script src="//fb.me/react-0.11.1.js"></script> <script src="//fb.me/JSXTransformer-0.11.1.js"></script></head> <style> #dayPicker { position: relative; top: 97px; left: 20px; width: 60px; height: 17px; } #monthPicker { position: relative; top: 97px; left: 22px; width: 95px; height: 17px; } select { font-size: 11px; } </style> <body> <div id="leftPanel"> </div> <script type="text/jsx" src="dates.js"></script> </body></html>
添加回答
举报
0/150
提交
取消