我正在尝试在成功执行自定义操作后刷新列表。
我在休息教程中使用了管理员的传奇
function * actionApproveSuccess () {
yield put(showNotification('Executed'))
yield put(push('/comments'))
// does not refresh, because the route does not change
// react-redux-router also has no refresh() method, like react-router has...
}
我的另一个想法是以某种方式触发列表组件的刷新操作,但我不知道如何访问它或如何将其挂钩到ACTION_SUCCESS事件。
答案 0 :(得分:5)
无法通过react路由器刷新路由,这是known problem。 Admin-on-rest的List
组件有its own refresh mechanism,但不提供API。
我的建议是使用基于admin-on-rest的自定义<List>
组件。如果您找到了公开refresh
操作的方法,请随意在aor存储库上打开PR!
答案 1 :(得分:4)
@Danila Smirnov上面的回答显示了我现在使用它时的消息:
弃用警告:刷新列表视图的首选方法是将自定义按钮与redux连接并调度refreshView操作。
现在点击刷新按钮本身也无法正常工作。
这是我在我的工作中调整过的版本。
编辑:对其进行修改以使其可重复使用。
<强> RefreshListActions.js 强>
AsyncSubject
在我的列表中,我想经常刷新:
import React, { Component } from 'react'
import FlatButton from 'material-ui/FlatButton'
import { CardActions } from 'material-ui/Card'
import NavigationRefresh from 'material-ui/svg-icons/navigation/refresh'
import { connect } from 'react-redux'
import { REFRESH_VIEW } from 'admin-on-rest/src/actions/uiActions'
import { refreshView as refreshViewAction } from 'admin-on-rest/src/actions/uiActions'
class MyRefresh extends Component {
componentDidMount() {
const { refreshInterval, refreshView } = this.props
if (refreshInterval) {
this.interval = setInterval(() => {
refreshView()
}, refreshInterval)
}
}
componentWillUnmount() {
clearInterval(this.interval)
}
render() {
const { label, refreshView, icon } = this.props;
return (
<FlatButton
primary
label={label}
onClick={refreshView}
icon={icon}
/>
);
}
}
const RefreshButton = connect(null, { refreshView: refreshViewAction })(MyRefresh)
const RefreshListActions = ({ resource, filters, displayedFilters, filterValues, basePath, showFilter, refreshInterval }) => (
<CardActions>
{filters && React.cloneElement(filters, { resource, showFilter, displayedFilters, filterValues, context: 'button' }) }
<RefreshButton primary label="Refresh" refreshInterval={refreshInterval} icon={<NavigationRefresh />} />
</CardActions>
);
export default RefreshListActions
答案 2 :(得分:2)
绝对是hacky,但解决办法可能是:
push('/comments/1') //any path to change the current route
push('/comments') //the path to refresh, which is now a new route
答案 3 :(得分:2)
通过redux使用refreshView操作效果很好。
参见示例....
import { refreshView as refreshViewAction } from 'admin-on-rest';
import { connect } from 'react-redux';
class MyReactComponent extends Component {
//... etc etc standard react stuff...
doSomething() {
// etc etc do smt then trigger refreshView like below
this.props.refreshView();
}
render() {
return <div>etc etc your stuff</div>
}
}
export default connect(undefined, { refreshView: refreshViewAction })(
MyReactComponent
);
答案 4 :(得分:1)
我通过“动作”面板通过小型黑客解决此任务。我确定这不是正确的解决方案,但在某些情况下它可以提供帮助:
class RefreshButton extends FlatButton {
componentDidMount() {
if (this.props.refreshInterval) {
this.interval = setInterval(() => {
this.props.refresh(new Event('refresh'))
}, this.props.refreshInterval)
}
}
componentWillUnmount() {
clearInterval(this.interval)
}
}
const StreamActions = ({ resource, filters, displayedFilters, filterValues, basePath, showFilter, refresh }) => (
<CardActions>
{filters && React.cloneElement(filters, { resource, showFilter, displayedFilters, filterValues, context: 'button' }) }
<RefreshButton primary label="Refresh streams" onClick={refresh} refreshInterval={15000} refresh={refresh} icon={<NavigationRefresh />} />
</CardActions>
);
export default class StreamsListPage extends Component {
render() {
return (
<List
{...this.props}
perPage={20}
actions={<StreamActions />}
filter={{ active: true }}
title='Active Streams'>
<StreamsList />
</List>
)
}
}
&#13;
答案 5 :(得分:1)
push
只是AOR的重定向,对我来说似乎也没有用。 guleryuz发布的内容对我来说是正确的轨道..这就是我在他的榜样上所做的事情:
// Import Statement
import { refreshView as refreshViewAction } from 'admin-on-rest';
class RemoveButton extends Component {
handleClick = () => {
const { refreshView, record, showNotification } = this.props;
fetch(`http://localhost:33333/api/v1/batch/stage/${record.id}`, { method: 'DELETE' })
.then(() => {
showNotification('Removed domain from current stage');
refreshView();
})
.catch((e) => {
console.error(e);
showNotification('Error: could not find domain');
});
}
render() {
return <FlatButton secondary label="Delete" icon={<DeleteIcon />}onClick={this.handleClick} />;
}
}
这些位也很重要:
RemoveButton.propTypes = {
record: PropTypes.object,
showNotification: PropTypes.func,
refreshView: PropTypes.func,
};
export default connect(null, {
showNotification: showNotificationAction,
refreshView: refreshViewAction,
})(RemoveButton);
所以它的工作方式是使用AOR&#39; s refreshViewAction
作为道具功能。这使用底层调用为我填充数据网格GET_LIST
。这可能不适用于您的特定用例。如果您有任何疑问,请告诉我。
答案 6 :(得分:0)
Pim Schaaf's解决方案对我来说就像一个魅力,我看起来有点不同
yield put(push('/comments/-1')); // This refreshes the data
yield put(showNotification('')); // Hide error