快速和“简单”:如何让我的父组件等待某个特定的状态,直到我每次访问父项时渲染子组件(不仅一次)?React Flux - 渲染子组件前等待道具
更详细一些: 我希望能够根据用户输入过滤结果。我有一个控制器视图“Items”,它调用一个名为“FilterComponent”的子组件,并将过滤器状态作为道具传递。我首先从控制器调用一个动作创建器,该动作创建器从本地存储获取已保存的过滤器并将其分发到商店。基于这个结果,我更新了过滤器状态并将其作为道具传递给了孩子。
我陷入困境的地方是找到一种方法来确保在设置过滤器状态之前不会呈现“FilterComponent”。我可以使它工作,这样当我刷新页面时,它会加载过滤器 - 但是当我访问其他地方的页面并导航到Items控制器时,它会卡住。
我认为它与我通过action/store获取过滤器的方式有关,它在componentDidMount()中调用 - 因为componentDidMount()只被调用一次,而且我需要调用它每次。
import React, { PropTypes } from 'react';
import FilterAction from '../../Actions/FilterActions';
import BaseComponent from '../../Components/Base';
import { Form, FormGroup, FormControl, ControlLabel, InputGroup, DropdownButton, MenuItem, Button } from 'react-bootstrap';
import Select from 'react-select';
import _ from 'lodash';
class FilterComponent extends BaseComponent {
constructor(props) {
super(props);
this.state = {};
this._bind(
'handleExperienceChange',
'handlePriceChange',
'applyFilter'
);
}
componentDidMount() {
this.applyFilter();
}
handlePriceChange(price) {
var filter = {};
var product = this.props.product.tag;
filter[product] = {
price: price ? price.value : null
};
let updatedState = _.merge(this.state, {
selectedPrice: price || null
});
this.updateFilter(filter);
this.setState(updatedState);
}
getPrice() {
var price = this.props.lang.select.prices;
var priceSelect = [];
Object.keys(price).map(function (key) {
priceSelect.push({
value: key,
label: price[key]
});
});
return priceSelect;
}
applyFilter() {
var filters = this.props.filters || null;
var product = this.props.product || null;
if (filters && product && filters[product.tag]) {
var selectedPrice = filter.price ? {
value: filter.price,
label: this.props.lang.select.prices[filter.price]
} : false;
}
var updatedState = {
selectedPrice: selectedPrice || false
};
this.setState(_.merge(this.state, updatedState));
}
updateFilter(filter) {
FilterAction.update(filter);
}
render() {
const { product, lang } = this.props;
return (
<div className="filter">
<div className="flex">
<div className="pane-50">
<Select name="price"
placeholder={lang.general.form.input.price}
value={this.state.selectedPrice}
options={this.getPrice()}
onChange={this.handlePriceChange}
searchable={false} />
</div>
</div>
</div>
);
}
}
export default FilterComponent;
你能不能在'Items' render方法中检查这个状态的存在,并且只有在状态被设置时才包含'FilterComponent'作为子对象?像(在jsx中)'{this.state.filters? :'Loading ...'}' –
感谢您的回应Bill!当我在该特定页面上进行浏览器刷新时,它可以正常工作,但当我在其他地方进入页面并导航到项目页面时,它在“正在加载...”时没有任何错误。也许我的孩子的组成部分是问题?我已经更新了我的答案,并包含了我的子组件的代码。我还注意到,当我点击刷新时,它会在父级调用render()3次,而第三个元素设置了过滤器,但是当我从另一个页面导航到它时,它只渲染两次,两次都过滤未定义。 – Dave
你的代码似乎在混合和匹配'this.props.filters'和'this.state.filters',这对我来说没有意义。在'updateFilters'中,你设置了'this.state.filters',但你永远不会在任何地方使用该状态;你在'applyFilter'中使用'this.props.filters'。这可能是你的问题吗? –