您好,我在获取数据并作为 this.props.data 传递给子组件时遇到问题 <GoogleMaps />并在数据更改时重新渲染。
它只在第一次提交表单和获取数据时起作用,但不会再次起作用。自 render() 以来,新数据不可用。函数调用 {this.renderMap(this.props.data)}之前{this.renderData()} ,它获取并返回并映射数据。
我尝试切换它们,但看起来 render() 函数在调用 {this.renderMap(this.props.data)} 之前没有等到数据被获取.我是否应该以不同的方式将数据传递给 GoogleMap 组件,以允许它在呈现之前重新加载新数据?
我的渲染数据();工作正常,返回组件 ListItem .每次提交我的表单时它都有效。问题是得到 GoogleMaps组件了解此更改并重新呈现?
class Feature extends Component {
handleFormSubmit({ term, location }) {
this.props.fetchData( { term, location });
this.setState({showMap: true});
}
renderData() {
return this.props.data.map((data) => {
return (
<ListItem
key={data.id}
data={data} />
)
});
}
renderMap(data) {
if (this.state.showMap == true) {
const lon = data[0].location.coordinate.longitude;
const lat = data[0].location.coordinate.latitude;
return (
<GoogleMap
data={data}
lon={lon}
lat={lat} />
)
}
}
render() {
const { handleSubmit, fields: { term, location }} = this.props;
return (
<div>
<div>
<form className="form-inline" onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<fieldset className="form-group">
<label>Find:</label>
<input className="form-control" {...term} />
</fieldset>
<fieldset className="form-group">
<label>Location:</label>
<input className="form-control" {...location} />
</fieldset>
<button action="submit" className="btn btn-primary">Search</button>
</form>
</div>
<div className="map_container">
{this.renderMap(this.props.data)}
</div>
<ul className="list-group">
{this.renderData()}
</ul>
</div>
)
}
}
function mapStateToProps(state) {
return { data: state.data };
}
export default reduxForm({
form: 'search',
fields: ['term', 'location']
}, mapStateToProps, actions)(Feature);
这是 fetchData 函数,我正在获取数据。
export function fetchData( {term, location} ) {
return function(dispatch) {
axios.post(`${ROOT_URL}/data`, {term, location})
.then(response => {
dispatch({
type: FETCH_DATA,
payload: response
})
})
}
}
最佳答案
终于解决了。 ComponentsWillReceiveProps 成功了。必须使用 nextProps 调用 ComponentsWillReceiveProps 并调用 setState 将数据设置为 nextProps.data。
这让我可以使用数据来呈现 GoogleMaps 经纬度坐标并设置每个标记。
关于javascript - React Redux,在获取数据并将其映射到组件后重新渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37335723/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el