如何在管理员休息时刷新列表视图
我想在自定义操作成功执行后获取要刷新的列表。如何在管理员休息时刷新列表视图
我使用的传奇从管理上休息教程
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事件。
无法通过反应路由器刷新路由,这是一个known problem。组件的List
组件有its own refresh mechanism,但不提供API。
我的建议是使用基于admin-on-rest的自定义<List>
组件。如果您找到了公开refresh
操作的方法,请随时在aor存储库上打开PR!
肯定哈克,但一个变通办法可以是:
push('/comments/1') //any path to change the current route
push('/comments') //the path to refresh, which is now a new route
我已经解决了通过操作面板的小黑客此任务。我敢肯定,这是不正确的解决办法,但在某些情况下,它可以帮助:
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>
)
}
}
@Danila横溢的回答上面显示这个消息时,我现在使用它:
弃用警告:刷新List视图的首选方法是将自定义按钮与redux连接起来,并分派refreshView操作。
单击刷新按钮本身现在不工作。
这是我在我的工作过的调整版本。
编辑:修改它多一点,使其可重复使用。
RefreshListActions.js
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
在我的名单,我要经常刷新:
import RefreshListActions from './RefreshListActions'
export default (props) => (
<List {...props}
actions={<RefreshListActions refreshInterval="10000" />}
>
<Datagrid>
...
</Datagrid>
</List>
)
这是行得通的。最好是创建一个具有自动更新间隔的下拉菜单的按钮,比如disable,10,50,100等等。我看到有人在其他帖子中有类似的想法,https://stackoverflow.com/questions/44551957/IS-有-A-方式对模拟压-的刷新按钮到刷新一个列表。但没有运气让它工作 –
通过终极版使用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
);
的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的refreshViewAction
为道具的功能。这使用底层调用为我填充数据网格,它是GET_LIST
。这可能不适用于您的具体使用情况。如果您有任何问题,请告诉我。
刚刚尝试过,它显示一个错误,并且出现以下提示:“弃用警告:刷新List视图的首选方法是将自定义按钮与redux连接起来,并分派'refreshView'动作。 –