这是reducer_posts.js来自非常简单的博客react-redux应用程序.
import _ from 'lodash';
import { FETCH_POSTS, FETCH_ONE_POST, DELETE_POST } from '../actions/index';
export default function (state = {}, action) {
switch (action.type) {
case DELETE_POST:
return _.omit(state, action.payload);
case FETCH_ONE_POST:
return { ...state, [action.payload.data._id]: action.payload.data };
case FETCH_POSTS:
return _.mapKeys(action.payload.data, '_id');
default:
return state;
}
}
Run Code Online (Sandbox Code Playgroud)
_.omit(state, action.payload) 是没有action.payload返回状态,所以返回状态没有删除帖子.
_.mapKeys(action.payload.data, '_id') 创建一个与初始对象具有相同值的对象,但是新对象具有从中获取的新键 action.payload.data._id
但我不能只是在代码中得到什么,这段语法确切地说:
return { ...state, [action.payload.data._id]: action.payload.data };
Run Code Online (Sandbox Code Playgroud)
这行代码有什么作用?这是什么意思?
或者我可以从git中删除它吗?因为添加新的提交只是因为index.test.js.snap改变而烦人...
我有这样的输入:
{
a: [obj1, obj2, obj3...],
b: [obj1, obj2, obj3...],
c: [obj1, obj2, obj3...]
}
Run Code Online (Sandbox Code Playgroud)
我想要该输出(每个键只有数组中的第一个对象)
{
a: [obj1],
b: [obj1],
c: [obj1]
}
Run Code Online (Sandbox Code Playgroud)
我想使用ramda.js来做
您能否以简单的方式向我解释在 React 代码中使用Classnames实用程序的目的是什么?我刚刚阅读了 Classnames 文档,但我仍然无法理解在代码中使用它的主要原因是什么:
import classnames from 'classnames';
[...]
render() {
const { className } = this.props
return (
<div className={classnames('search', className)}>
<div className="search-bar-wrapper">
<form className="search-form" onSubmit={this.onSearch.bind(this)}>
<div className="search-bar">
<label className="screen-reader-only" htmlFor="header-search-form">Search</label> [...]
Run Code Online (Sandbox Code Playgroud)
此代码的完整版本(jsx):https ://jsfiddle.net/John_Taylor/j8d42m2f/2/
我不明白这行代码发生了什么:
<div className={classnames('search', className)}>
Run Code Online (Sandbox Code Playgroud)
我也读过那个( 如何为 React.js 使用类名库)的答案,但我在理解我的代码片段方面仍然有问题。
我有这样的标记:
HTML:
<ListElementRoot>
<ListElementText>
{children}
</ListElementText>
<ListElementDescription>
{description}
</ListElementDescription>
</ListElementRoot>
Run Code Online (Sandbox Code Playgroud)
//平台.web.js
export const ListElementRoot = div(style.root)
export const ListElementIcon = div(style.icon)
export const ListElementText = div(style.text)
export const ListElementDescription = div(style.description)
Run Code Online (Sandbox Code Playgroud)
//CSS
.root {
display: flex;
width: 100%;
height: 56px;
align-items: center;
border-top: 1px solid var(--color-gray2);
padding: 0;
}
.icon {
position: absolute;
width: 28px;
height: 28px;
align-items: center;
padding-left: 18px;
}
.text {
color: #000;
font-size: calc(var(--font-size-body) / 10)rem;
padding-left: 64px;
}
.description {
color: #000;
font-size: calc(var(--font-size-body) / …Run Code Online (Sandbox Code Playgroud) javascript ×3
reactjs ×2
css ×1
enzyme ×1
flexbox ×1
jestjs ×1
npm ×1
ramda.js ×1
react-redux ×1
redux ×1
testing ×1
unit-testing ×1