我有map功能:
const map = <T, U>(f: (x: T) => U, arr: T[]): U[] => {
return arr.map((val) => f(val));
}
Run Code Online (Sandbox Code Playgroud)
当我map使用匿名函数作为回调调用时,它的返回类型是正确的:
// `x1` variable type here is { name: string }[], which is correct
const x1 = map(x => x, [{name: 'John'}]);
Run Code Online (Sandbox Code Playgroud)
但是当我提供identity函数而不是匿名函数时,返回类型是错误的:
const identity = <T>(x: T) => x
// return type of `x2` is {}[] here
const x2 = map(identity, [{name: 'John'}]);
Run Code Online (Sandbox Code Playgroud)
如何为第二个例子获得正确的类型结果,而不为map函数提供显式类型参数?
在redux-observable,史诗正在接受行动流并返回新的行动流.在我的用例中,我需要在调度某个操作后发送分析事件,之后不执行任何操作.
有了redux-saga,我可以使用takeEvery,并在saga函数内部执行副作用:
function* saga() {
yield takeEvery('SOME_ACTION', function*() {
sendAnalytics();
})
}
Run Code Online (Sandbox Code Playgroud)
但我怎样才能实现同样的目标redux-observable呢?有很多副作用,不需要调度新的动作,比如初始化插件,记录,设置cookie等......
如果它是这两个库的反模式,那么应该使用什么解决方案来实现这些效果呢?
用例是我想"毫不费力地"将某些prop值传递给所有后代组件.不确定这在React中是否可行.
所以不要这样做:
class Parent extends Component {
constructor() {
super(props);
this.props.componentID = "123456";
}
render() {
return <Child1 componentID={this.props.componentID} />
}
}
class Child1 extends Component {
render() {
return <Child2 componentID={this.props.componentID} />
}
}
class Child2 extends Component {
render() {
return <div>{this.props.componentID}</div>
}
}
Run Code Online (Sandbox Code Playgroud)
做这样的事情:
class Parent extends Component {
constructor() {
this.props.componentID = "123456";
}
passComponentIDToAllDescendantComponents() {
// Some super nifty code
}
render() {
return <Child1 />
}
}
// etc...
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助
有没有办法使用 GitHub API 或任何外部服务从 GitHub 上的组织中获取所有贡献者?
我正在尝试angular使用 GitHub API从组织中获取所有贡献者。
我只找到了一种解决方案:
angular使用此请求从组织获取所有存储库:
GET https://api.github.com/orgs/angular/repos
Run Code Online (Sandbox Code Playgroud)对于每个 repo,通过此请求获取其所有贡献者:
GET https://api.github.com/repos/angular/:repo/contributors
Run Code Online (Sandbox Code Playgroud)它似乎有效,但我认为这个解决方案非常麻烦。我以这种方式发送了大约 300 个请求,它们正在处理大约 20 秒(应用程序将被冻结,直到所有请求都没有完成)。
问题:
我遇到一个问题,如果字符串没有斜杠,我需要添加前导斜杠和尾随斜杠,或者如果字符串已经有斜杠,则不执行任何操作。
例如:
"/path" => "/path/"
"path/" => "/path/"
"path" => "/path/"
"/path/" => "/path/"
"/" => "/"
"" => "/"
Run Code Online (Sandbox Code Playgroud)
我尝试使用这个正则表达式,但它没有添加尾部斜杠:
'/path'.replace(/(^\/?)|(\/?$)/, '/'); // output is "/path"
Run Code Online (Sandbox Code Playgroud) 我的应用程序中有多个实体.每个实体都独立于其他实体.所以我为每个实体创建了一个单独的reducer,并在创建store时组合它们.
每个条目都有一些常见的操作,如
1)设置项目清单2)添加单项3)更新单项4)删除单项
所以在每个reducer中我都有相同类型的代码重复
设置清单
return {
...state,
list: {
...state.list,
...action.list
}
};
Run Code Online (Sandbox Code Playgroud)
添加单个项目
return {
...state,
list: {
...state.items,
[action.item.id]: {
...state.list[action.item.id],
...action.item
}
}
};
Run Code Online (Sandbox Code Playgroud)
..更新操作和删除操作的相似代码
有没有我可以抽象出这个代码,并为每个实体保留一个单独的reducer?
javascript ×3
redux ×2
children ×1
descendant ×1
github ×1
github-api ×1
react-redux ×1
reactjs ×1
redux-saga ×1
regex ×1
rxjs ×1
typescript ×1