- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
基于脚手架mern.io我正在检查代码以查看发生了什么。我偶然发现了一个 .need 方法,它看起来像与 es6 类相关的东西。我似乎无法在任何地方找到任何可用的信息,所以我问 .need
方法是什么?
class PostContainer extends Component {
//do class setup stuff here
}
PostContainer.need = [() => { return Actions.fetchPosts(); }];
您可以使用这些命令轻松启动并运行项目。
npm install -g mern-cli
mern YourAppName
最佳答案
在解释这一点时,mern 文档非常简洁。
fetchComponentData
collects all the needs (need is an array of actions that are required to be dispatched before rendering the component) of components in the current route. It returns a promise when all the required actions are dispatched.
通读代码可以更清楚地了解这里发生了什么。
这是一种指定在呈现组件之前应分派(dispatch)的一些操作的方法。
此组件将 posts
属性从 Redux 存储映射到名为 posts
的 prop,以便它可以呈现帖子列表。
// PostContainer.jsx
function mapStateToProps(store) {
return {
posts: store.posts,
};
}
但是,最初此属性将为空,因为需要从异步 API 获取帖子。
// reducer.js
// initial state of the store is an empty array
const initialState = { posts: [], selectedPost: null };
此组件需要帖子在呈现之前可用,因此它会将调用返回的操作分派(dispatch)给 Actions.fetchPosts()
。
// actions.js
export function fetchPosts() {
return (dispatch) => {
return fetch(`${baseURL}/api/getPosts`).
then((response) => response.json()).
then((response) => dispatch(addPosts(response.posts)));
};
}
当 Action 完成调度时,商店的数据可以映射到连接的组件。
这不是为 React 组件指定异步依赖项的通用方法。它之所以有效,是因为 mern 有一个名为 fetchComponentData
的实用方法,它会在服务器端调用该方法,以便在渲染之前填充 Redux 存储。
// server.js
fetchComponentData(store.dispatch, renderProps.components, renderProps.params)
此方法遍历来自第二个参数的组件,以从每个组件中提取需求
。然后它执行“需要”并等待所有 promise 完成。
// fetchData.js
const promises = needs.map(need => dispatch(need(params)));
return Promise.all(promises);
当 Promise.all(promise)
返回的 promise 完成时,Redux 存储将被填充,组件可以安全地呈现它们的数据以提供给客户端。
您提到您认为它可能与 ES6 类有关,所以我也会快速介绍语法。
ES6 类不能在类字面量中指定静态属性,相反我们必须在类定义后将它们声明为类的属性。
needs 属性必须是一个函数数组,这些函数返回与 fetchComponentData
一起工作的 promise 。在这种情况下,我们在数组文字中声明了一个箭头函数。将其拆分为单独的变量可能会有所帮助。
const fetchPosts = () => { return Actions.fetchPosts() };
const needs = [fetchPosts];
PostContainer.need = needs;
关于javascript - 使用 mern.io 脚手架工具 - .need 方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35614382/
我读了here您可以使用命令构建模块,因此您无需手动创建一些初始文件。但是这样的命令在 master 上不起作用(在 Odoo 开发分支上): ./oe scaffold Academy ../my-
我是 Grails 新手。我有一个 Person 域类: class Person { String firstName String lastName String gend
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我使用 yeoman 作为脚手架工具,但出现以下错误。任何人都可以帮我解决这个问题。我正在使用 Windows 8 环境。 karma-jasmine@0.1.5 node_modules\karma
我正在查看使用grails generate-all生成的 Controller 。为什么同时存在create和save操作,是否有原因?对于我来说,仅将create作为操作并将save作为服务对我来
我是个新手。我现在正在研究脚手架模板,尤其是在 Controller 上。我每次生成时都想要一个自定义的 Controller ,所以我使用了“安装模板”。我总是在 Controller 上创建Com
我有一个类库,其中包含一些模型类和一个 DbContext 类(所有这些类都是公共(public)的)。该类库由 MVC-5 应用程序引用。 是否可以使用该引用类库中的模型类来构建该 MVC-5 应用
我有一个使用 Twitter Bootstrap 构建的单页应用程序。我的应用程序有几个模态对话框,它们通常占页面宽度的 70%,并且水平居中。我的页面和模态设置是这样的: 我在模态框内放置了一行和几
我正在开发一个具有某种“协调器”界面的网络应用程序。我希望能够支持我正在使用的 Eyefinity 设置(即 3 个纵向显示器,分辨率为 3600x1920,或 3780x1920,并启用了边框校正)
在处理 codeigniter 中的脚手架时,我有哪些选择 - 人们经常使用脚手架吗?或者是否有更好的方法来快速生成用于 CRUD 应用程序的代码? 我已经安装了 Spark 塞:http://cod
我正在我工作的公司解决方案中实现存储库模式,将后端项目中的模型类与 DbContexts 项目中的数据库上下文和迁移分开。 我正在使用 Scaffold-DbContext 将我的后端项目设置为模型类
目录 1、前言 2、什么是脚手架呢? 3、谁提供了脚手架? 1、Spring 官网脚手架 2、阿里云脚手架 4、手撸一个脚手架!
我在脚手架中有一个应用栏。 return Scaffold( appBar: styling.appBar( AppBar( leading: styling.icon
我正在尝试使用 Compose 设计一个布局,其中包括: 热门应用栏 正文(内容) 底部应用栏 单击时表示菜单的底部表(模态底部表) -------TopAppBar------ ------主要内容
有谁知道如何为所有对象属性“构建”或“生成”构造函数块?我有 VS2010 和 Resharper,我希望生成类似的东西: public Customer CustomerB = new Cu
我正在尝试在 grails 中上传文件。生成的脚手架对我不起作用,给我一个 404 寻找“save.jsp” 域名 class Simple{ byte [] avatar static co
因为我已经手动调整了我的实体和 Controller 以及 View ,所以我希望 roo 不更改它们中的任何一个。尽管如此,我还是希望继续为所有新实体搭建脚手架。我怎么能那样做 最佳答案 您可以通过
我使用命令grails generate-restful-controller Domain创建了一个 Restful Controller ,如何在创建的 Controller 中设置scaffol
我有一个定义了 staticscaffold = true 的 Controller ,以及一些自定义操作。 我想确保只有登录用户和 ADMIN 类型的用户(我们域中的某些 Enum 值)才能访问它。
我有一个表,其中某些列的值相对较长,并且在我的 Grails 应用程序中为其打开了动态脚手架(我使用的是 Grails 3.3.8)。因此,在某些屏幕分辨率下,它们不适合屏幕,并且最右边的列最终会出现
我是一名优秀的程序员,十分优秀!