[英]Immutable JS - get a value inside a nested map
我使用immutableJS,反应,和/终极版,我有这个地图,我需要得到的值actions
,所以它会像- > allRetrospectivesMap
- > id
- > current_stage
- > actions
我有这段代码并且可以正常工作,但是非常丑陋,有没有更好的方法呢?
class UserActionItems extends Component {
render() {
const { retrospectives } = this.props
const actions = flatten(
Object.keys(immutableHelpers.toJS(retrospectives))
.map(key => retrospectives.get(key))
.map(retro => immutableHelpers.toJS(retro.getIn(['current_stage', 'actions'])))
).value()
return (
<div>
<ActionsList
actions={actions[0]}
users={[]}
/>
</div>
)
}
}
const mapStateToProps = ({ allRetrospectivesMap }) => ({
retrospectives: allRetrospectivesMap
})
谢谢!!! :)
您可以使用不可变js中的getIn()
方法来执行此操作。
const id = getId() // you've id from somewhere
const actions= state.getIn(['allRetrospectivesMap', id, 'current_stage', 'actions']); // Note: state is your immutable data.
在这里了解更多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.