简体   繁体   English

用数据初始化React组件的正确方法是什么?

[英]What is the correct way of initializing react component with data?

I'm learning react.js with rails using react-rails . 我正在使用react-rails使用rails学习react.js。 App is basically list of sport events. 应用程式基本上是体育赛事清单。

What is better way of initializing my main component with data? 用数据初始化我的主要组件的更好方法是什么?

  1. <%= react_component('EventsBox', {url: events_path(format: 'json')}, {prerender: true)} %>

And then make an ajax request to upload data from this url in componentDidMount . 然后发出ajax请求以从componentDidMount该URL上传数据。

or 要么

  1. send it directly via props? 通过道具直接发送?

<%= react_component('EventsBox',{data: Event.all.as_json},{prerender: true)} %>

如果要使用数据进行预渲染,则选项2是您想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从 Rails 视图传递到 Rails 6 中的反应组件的首选方法是什么? - What is the preferred way to pass data from a Rails view to a react component in Rails 6? 在React中的html元素内添加onChange事件的正确方法是什么? - What is correct way to add onChange event inside html element in React? 关联和呈现来自多个模型的数据的正确方法是什么? - What's the correct way to associate & render data from multiple models? 为表中显示的数据数组创建React组件的最佳方法? - Best way to create a react component for an array of data displayed in a table? 有没有一种方法可以使用ERB在ERB中渲染React组件 <Component/> ? - is there a way to render a React component in ERB using <Component/>? 从yaml文件中读取一些数据并在erb页面中使用它的正确方法是什么? - What's the correct way to read some data from a yaml file and use it in an erb page? 当数据存储在一个对象上但只能由另一个对象操纵时,对它进行建模的正确方法是什么? - When data is stored on one object but can only be manipulated by another what's the correct way to model it? 在 Rails 4 中验证循环关联的正确方法是什么? - What is the correct way to validate Circular Associations in Rails 4? 遍历此块的正确方法是什么? - What is the correct way to loop through this block? 在Rails 4中,将.where与关联一起使用的正确方法是什么? - In rails 4 what is the correct way of using .where with associations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM