我正在用Koa构建API。我用koa-router设置了所有路由。每条路线都使用一个控制器,该控制器具有给定猫鼬模型的所有逻辑。我已经阅读了有关错误处理的Koa文档,并了解了await在try/catch块中使用ing的方法。他们提到在中间件链的开始应设置一个默认错误处理程序。因此,如果要执行以下操作,则应该对处的路由进行合理的错误处理router.get():
const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
err.status = err.statusCode || err.status || 500;
throw err;
}
});
router
.get('/', async (ctx, next) => {
console.log('Got Route');
//ctx.body = users;
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000, () => console.log('Koa app listening on 3000'));
Run Code Online (Sandbox Code Playgroud)
如果我要在这条路线上做些更复杂的事情,那么try/catch …
我正在学习 React 16.3,它是新的 Context API。特别是从嵌套组件更新上下文。在他们的示例中,他们设置了一个在构造函数中定义的方法而不是标准方法。
class App extends React.Component {
constructor(props) {
super(props);
// What is the benefit of doing this here?
this.toggleTheme = () => {
this.setState(state => ({
theme:
state.theme === themes.dark
? themes.light
: themes.dark,
}));
};
this.state = {
theme: themes.light,
toggleTheme: this.toggleTheme,
};
}
render() {
// The entire state is passed to the provider
return (
<ThemeContext.Provider value={this.state}>
<Content />
</ThemeContext.Provider>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我读过的关于提升状态和将方法传递给孩子的所有内容都是使用以下模式完成的。为什么以上优先于以下?有什么区别吗?
class App extends React.Component {
constructor(props) { …Run Code Online (Sandbox Code Playgroud) 我试图找出为什么汇总不更新。在我的包文件中我有:
"devDependencies": {
"rollup": "^0.62.0",
}
Run Code Online (Sandbox Code Playgroud)
跑步npm outdated节目:
package Current Wanted Latest
rollup 0.62.0 0.62.0 0.67.1
Run Code Online (Sandbox Code Playgroud)
如果克拉表示除主要版本之外的所有内容都可以更新,并且在这种特定情况下最新版本只是次要版本,为什么不使用 进行更新npm update rollup?
我意识到通缉令正在阻止更新,但为什么呢?
我正在尝试使用 Rollup 作为 React 工具集。当我可以在反应中使用延迟加载时,我有一个工作原型。我只使用 ES6 模块,稍后我会为旧浏览器集成 SystemJS。我的问题是关于汇总设置。将reactandreact-dom视为内部似乎很疯狂,然后必须提供 amanualChunk并再次将它们分开。
如果我将它们视为外部而不是将它们分块,则会出现以下错误:
Uncaught (in promise) TypeError: Failed to resolve module specifier "react-dom". Relative references must start with either "/", "./", or "../".
Run Code Online (Sandbox Code Playgroud)
有没有办法将react和react-dom视为外部并从 CDN 引用它们?
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import json from 'rollup-plugin-json';
import replace from "rollup-plugin-replace";
export default {
input: ['src/index.js'],
output: {
format: 'esm',
dir: 'build',
},
experimentalCodeSplitting: true,
// Is there …Run Code Online (Sandbox Code Playgroud) 我正在尝试在窗口调整大小事件上测量 React DOM 节点。我在React hooks-faq上使用过这个例子,但它只发生在第一次渲染中。如果我添加一个useEffect来监听调整大小,回调不会被调用?
function MeasureExample() {
const [height, setHeight] = useState(0);
const measuredRef = useCallback(node => {
if (node !== null) {
setHeight(node.getBoundingClientRect().height);
}
}, []);
// Adding this effect doesn't re-calculate the height???
useEffect(() => {
window.addEventListener("resize", measuredRef);
return (): void => {
window.removeEventListener("resize", measuredRef);
};
}, [height])
return (
<>
<h1 ref={measuredRef}>Hello, world</h1>
<h2>The above header is {Math.round(height)}px tall</h2>
</>
);
}
Run Code Online (Sandbox Code Playgroud) 我有一个原始数组,如下所示:
const original = [
{
type: 'foo'
},
{
type: 'foo'
},
{
type: 'bar'
}
]
Run Code Online (Sandbox Code Playgroud)
然后我通过 lodash groupBy 和 iteratee 运行它,(val) => val.type得到:
{
bar: [{
type: "bar"
}],
foo: [{
type: "foo"
}, {
type: "foo"
}]
}
Run Code Online (Sandbox Code Playgroud)
有没有一种 lodash 方法可以将每个键的属性转换/减少到它们的 Array.length 中?
{
bar: 1,
foo: 2
}
Run Code Online (Sandbox Code Playgroud)