我有一个组件,当它被单击时,我试图将数据从前一个组件传递到该组件。我尝试过使用setState来改变状态,但是什么都不起作用。如何传递数据?数据存储在this.listing中
以下是第一个组件:
const JobOffer = React.createClass({
mixins: [Navigation],
getInitialState: function() {
console.log(this.props.data);
return {
listing: this.props.data
};
},
componentDidMount: function() {
AppStore.addChangeListener(this._onChange);
},
componentWillUnmount: function() {
AppStore.removeChangeListener(this._onChange);
},
_onChange : function(){
},
handleClick: function () {
this.transitionTo('/listing/' + this.state.listing.id );
},
render: function () {
var data = this.state.listing;
var employmentType;
switch(data.employment_type) {
case 'F':
employmentType = 'Full Time';
break;
case 'P':
employmentType = 'Part Time';
break;
case 'H':
employmentType = 'Hourly';
break;
default:
employmentType = 'Unknown';
}
return (
<div>
<a onClick={this.handleClick.bind(this)}>
<img style={{width: 50+'px', height: 50+'px'}} src="/images/job1.jpg" alt="" className="img-circle" />
<div className="title">
<h5>{data.job_title}</h5>
<p>{data.Business.business_name}</p>
</div>
<div className="data">
<div ><i>Posted 1 Day Ago</i></div>
<div className="city"><i className="fa fa-map-marker"></i>{data.Business.business_city}</div>
<div className="type full-time"><i className="fa fa-clock-o"></i>{employmentType}</div>
<div className="sallary"><i className="fa fa-dollar"></i>{data.job_compensation}</div>
</div>
</a>
</div>
);
},
});
module.exports = JobOffer;正如您所看到的,transitionTo上面的组件是我想要的路由,但是在调用handleClick时如何传递this.listings数据呢?
下面是我试图将其传递给的组件:
var React = require('react');
var ReactBootstrap = require('react-bootstrap');
var Button = ReactBootstrap.Button;
var Modal = ReactBootstrap.Modal;
var AppActions = require('../actions/app-actions');
var AppStore = require('../stores/app-store');
var Navigation = require('react-router').Navigation;
var _ = require('lodash');
// Our custom component is managing whether the Modal is visible
const ListingDetail = React.createClass({
mixins: [Navigation],
getInitialState: function() {
console.log(this.props.data);
return {
listing: this.props.data
};
},
componentDidMount: function() {
AppStore.addChangeListener(this._onChange.bind(this));
},
_onChange : function(){
},
handleClick: function () {
this.transitionTo('/listing/apply/' + this.state.listing.id );
},
render: function () {
var data = this.state.listing;
var employmentType = 'test';
return (
<div>
<img style={{width: 200+'px', height: 200+'px'}} src="/images/job1.jpg" alt="" className="img-circle" />
<div className="title">
<h5>{data.job_title}</h5>
<p>{data.Business.business_name}</p>
</div>
</div>
);
},
});
module.exports = ListingDetail;发布于 2015-07-13 19:25:07
React本身并不提供在组件之间共享数据的方法,除非它们位于相同的组件层次结构中。只有祖先到后代的传递是由React直接提供的,通过"props“将数据从父组件传递到子组件。您还可以通过将回调(在父级中定义的方法)作为道具传递给子组件,然后使子组件在发生更改时调用该回调并将数据作为参数传递,从而间接地将数据从子组件共享到父组件。阅读有关呈现多个组件和数据共享here的更多信息。
如果您的组件在完全不同的路径中呈现,就像您正在尝试做的那样,那么您将不得不以其他方式提供数据流。React本身没有提供这一点,但是Flux是Facebook的开发人员支持的一种方法,它明确地与处理数据流有关,并且非常适合React。
发布于 2015-07-13 08:51:13
这类问题正是Flux被设计出来的原因。不要试图将数据直接从一个组件传递到另一个组件,而是让存储保存数据。然后,当您转换到另一个组件时,该组件可以从存储区读取数据。
https://stackoverflow.com/questions/31331636
复制相似问题