- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在同一个组件上有搜索过滤器和排序输入。我正在使用 reselect(selector package)过滤和排序数据数组的位置。 mapStateToProps 在每个搜索过滤器结果上更新组件。但是 mapStateToProps 在对数组排序后不更新组件。
选择器/index.js
import { createSelector } from 'reselect'
const getListOfCategory = (state) => state.products.product
const getSearchText = (state) => state.products.searchText
const getSortValue = (state) => state.products.sortValue
export const getVisibleCategory = createSelector(
[ getListOfCategory, getSearchText, getSortValue ],
(ListOfCategory, searchText, sortValue) =>{
if((searchText !== undefined) && (searchText !== null) && (searchText !== "")){
return ListOfCategory.filter((val) => val.modelname.toLowerCase().includes(searchText.toLowerCase())).sort((a, b) => {
if (sortValue === 'likes') {
return b.modellikes - a.modellikes;
}
if (sortValue === 'views') {
return b.modelviews - a.modelviews;
}
if (sortValue === 'brand') {
return a.modelname > b.modelname ? 1 : a.modelname < b.modelname ? -1 : 0;
}
});
}
if(sortValue){
return ListOfCategory.sort((a, b) => {
if (sortValue === 'likes') {
return b.modellikes - a.modellikes;
}
if (sortValue === 'views') {
return b.modelviews - a.modelviews;
}
if (sortValue === 'brand') {
return a.modelname > b.modelname ? 1 : a.modelname < b.modelname ? -1 : 0;
}
});
}
}
)
这是我的 react 组件。
import React from 'react';
import {connect} from 'react-redux';
import {getCategoryInfo,search,sortBy} from '../actions/productActions';
import {getVisibleCategory} from '../selectors';
import {Link} from 'react-router-dom';
class SmartCategory extends React.Component{
constructor(props){
super(props);
this.state={
searchFilter:""
}
this.searchHandle=this.searchHandle.bind(this);
this.sortHandle=this.sortHandle.bind(this);
}
searchHandle(e){
this.setState({
searchFilter:e.target.value
});
this.props.search(e.target.value);
}
sortHandle(e){
this.props.sortBy(e.target.value);
}
componentDidMount(){
this.props.getCategoryInfo(this.props.page,this.props.pageId);
}
componentWillReceiveProps(nextProps){
if(nextProps.page !== this.props.page || nextProps.pageId !== this.props.pageId){
this.props.getCategoryInfo(nextProps.page,nextProps.pageId);
}
}
changeText(){
if(this.props.categoryTitle != undefined){
let categoryTitle=this.props.categoryTitle.charAt(0).toUpperCase() + this.props.categoryTitle.slice(1);
categoryTitle=categoryTitle.split('_').join(' ');
return categoryTitle;
}
}
render(){
const {error,isLoading}=this.props
if(error) return <ResourceNotFound error={error}/>;
if(isLoading) return <div className="spinner"></div>;
return (
<div className="container-fluid mb-4 categoryWrapper">
<div className="row mx-0 pt-4">
<div className=" col-sm-2 col-md-2 col-lg-2 px-2">
<div className="input-group mb-3">
<div className="input-group-prepend">
<span className="input-group-text" id="basic-addon1"><i className="fa fa-search" aria-hidden="true"></i></span>
</div>
<input type="text"
className="form-control"
placeholder="Search by brand"
onChange={this.searchHandle}
value={this.state.searchFilter}
/>
</div>
</div>
<div className=" col-sm-2 col-md-2 col-lg-2 px-2">
<div className="form-group">
<select className="form-control" id="sel1"
placeholder="-- Sort By -- "
onChange={this.sortHandle}
>
<option disabled>-- Sort By --</option>
<option value="brand" >Brand</option>
<option value="likes">Likes</option>
<option value="views">Views</option>
</select>
</div>
</div>
</div>
<div className="row mx-0 py-3">
{this.props.isLoading ? <div className="spinner"></div> : null}
{this.props.product && this.props.product.map((product,i)=>
<div className="col-sm-3 col-md-3 col-lg-3" key={product.id}>
<Link to={`/SmartView/${this.props.page}/${product.modelname}`} className="routeDecorator">
<div className="border mb-2 p-3 rounded ">
<span className="text-grey">
<span className="mr-2">
<i className="fa fa-thumbs-o-up" aria-hidden="true"> {product.modellikes}</i>
</span>
<span className="float-right">
<i className="fa fa-eye" aria-hidden="true"> {product.modelviews} views</i>
</span>
</span>
</div>
</Link>
</div>
)}
</div>
</div>
);
}
}
function mapStateToProps(state){
const {isLoading,isLoaded,error,categoryTitle,product,searchText,sortValue} = state.products
return {
isLoading,
error,
product:getVisibleCategory(state) || product,
categoryTitle
}
}
export default connect(mapStateToProps,{getCategoryInfo,search,sortBy})(SmartCategory);
最佳答案
MapStateToProps
对每次返回的值进行浅比较。浅比较将比较对象和数组的引用。排序时,数组就地排序,因此引用不会改变。参见 sort .您可以通过返回对数组的新引用来证明这一点:
return [...ListOfCategory.sort(a,b) // rest of sort code ]
关于javascript - react 堆 :mapStateToProps not rendering the component on state change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48475537/
我有以下代码。我无法理解应如何实现 ProjectCardDescription 组件,以便能够在 ProjectCard 组件中传递其描述 我尝试过这个,但得到一个空组件: import React
我们正试图从 styled-components 项目中找出以下问题的原因:https://github.com/styled-components/styled-components/issues/
将所有文件从 jsx 更改为 tsx 后,出现此错误: ./src/components/index.js Module not found: Can't resolve './Header' in
我正在努力遵循以下 vuejs 应用场景动态组件 + 异步组件模式。一切正常,但仍然只有一个问题:我怎样才能访问通过传入的 Prop 数据 请看现场 fiddle : https://jsfiddl
我已经明白了Difference between React Component and React Element , 使用 JSX 基本上调用 React.createElement它返回一个元素
我最近开始使用 JSX 元素语法而不是调用函数,因为它使代码更漂亮。但看起来又不太一样。令人惊讶的是,因为在 App.js 中,函数调用会导致无限循环(并引发错误),但 JSX 元素可以工作。在 Da
通过少量渲染来构建嵌套组件系统的好方法是什么?请参阅下面带有主要问题(“如何...”)的所需代码: tab.vue(子组件) export default {
我正在编写一个轻量级游戏引擎,并且在为它做一些研究的同时,我遇到了许多令人信服的文章,它们提倡通过“组件集合”模型而不是“从具体类继承”模型来实现游戏对象。有很多优点: 可以使用数据组合对象 驱动设计
类型‘AbstractControl’上不存在属性‘Controls’。
考虑以下示例: function Travel(props) { return ( ) } function Welcom
我刚刚加入了一个 React Native 项目,在那里我经常看到扩展 React.Component 和 Component 本身的类。 示例: 类 SomeView 扩展了 React.Compo
我见过两种访问 Component 的方法: import React from 'react'; class Foo extends React.Component { ... } 和 im
我有一个库 jar,我想将其提供给许多应用程序。我想要的行为是在库中创建一个通用的 spring 组件类。如果在应用程序中,没有扩展相同的组件,则使用公共(public)组件;如果它在应用程序中扩展,
所以我正在制作一个游戏,我有 EnemyAI 以及 player,它们都扩展了 JPanel。世界有一个 null 布局,所以我正在使用 setBounds(); 来“移动”(我实际上只是移动世界图像
在 styled-component 中,您如何决定是应该使用插值函数来修改组件(通过传递 props )还是扩展现有组件。例如: const Button = styled.button`
我搜索并获取了以下信息。 请添加您的信息 h:commandbutton 与 a4j:commandButton 相同,唯一的区别是 a4j:commandButton 有额外的 ajax 请求。 a
我目前在一个项目中,我们有一个动态“表单”/内容模型,其中我们有一个包含字段和占位符的模块,占位符可以包含更多模块,为我们提供递归/灵活的数据模型. 现在为了渲染这个,我们创建了一个组件来渲染模块,动
我是 React 的新手,正在尝试设置一个 Bootstrap 模式来显示警报消息。 在我的父 App.js 文件中,我有一个错误处理程序,它向 Modal.js 组件发送一个触发模态显示的 Prop
通过 background-color:red 获得主页组件写入其 scss,然后使用 background-color:green 获取用户组件写入它的 scss。我启动我的应用程序,我在家,背景是
我有这个基本的应用程序,其中一些组件具有公共(public) load 方法。对于某些操作,我想在当前 svelte:component 上调用该方法,但我不知道如何获取对组件实例的引用。如何做到这一
我是一名优秀的程序员,十分优秀!