Javascript 如何将数据推送到react管理存储?
我正在尝试使用react admin构建一个web应用程序,需要将数据推送到redux商店。我阅读React admin docs(),当我尝试这样做时,我会失败 这是我的密码Javascript 如何将数据推送到react管理存储?,javascript,reactjs,redux,react-admin,Javascript,Reactjs,Redux,React Admin,我正在尝试使用react admin构建一个web应用程序,需要将数据推送到redux商店。我阅读React admin docs(),当我尝试这样做时,我会失败 这是我的密码 import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { withStyles, MuiThemeProvider, creat
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { withStyles, MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import {
Menu,
Notification,
Sidebar,
setSidebarVisibility,
} from 'react-admin';
import {kidsLoad} from './customActions/KidsActions'
import AppBar from './MyAppBar';
const styles = theme => ({
root: {
display: 'flex',
flexDirection: 'column',
zIndex: 1,
minHeight: '100vh',
backgroundColor: theme.palette.background.default,
position: 'relative',
},
appFrame: {
display: 'flex',
flexDirection: 'column',
overflowX: 'auto',
},
contentWithSidebar: {
display: 'flex',
flexGrow: 1,
},
content: {
display: 'flex',
flexDirection: 'column',
flexGrow: 2,
padding: theme.spacing.unit * 3,
marginTop: '1em',
paddingLeft: 5,
},
});
class MyLayout extends Component {
componentWillMount() {
this.props.setSidebarVisibility(true);
}
componentDidMount(){
const { kidsLoad, record } = this.props;
kidsLoad({data: "HELLOOOOOOOOOOOO!!!!!"})
}
render() {
const {
children,
classes,
dashboard,
isLoading,
logout,
open,
title,
} = this.props;
return (
<div className={classes.root}>
<div className={classes.appFrame}>
<AppBar title={title} open={open} logout={logout} />
<main className={classes.contentWithSidebar}>
<div className={classes.content}>
{children}
</div>
</main>
<Notification />
</div>
</div>
);
}
}
MyLayout.propTypes = {
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
dashboard: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
]),
isLoading: PropTypes.bool.isRequired,
// logout: componentPropType,
setSidebarVisibility: PropTypes.func.isRequired,
title: PropTypes.string.isRequired,
kidsLoad: PropTypes.func,
};
const mapStateToProps = state => ({ isLoading: state.admin.loading > 0 });
export default connect(mapStateToProps, { setSidebarVisibility, kidsLoad })(withStyles(styles)(MyLayout));
import React,{Component}来自'React';
从“道具类型”导入道具类型;
从'react redux'导入{connect};
从“@material ui/core/styles”导入{withStyles,MuiThemeProvider,createMuiTheme};
进口{
菜单,
通知,
侧边栏,
设置可视性,
}来自“react admin”;
从“./customActions/KidsActions”导入{kidsLoad}
从“/MyAppBar”导入AppBar;
常量样式=主题=>({
根目录:{
显示:“flex”,
flexDirection:'列',
zIndex:1,
最小高度:“100vh”,
backgroundColor:theme.palette.background.default,
位置:'相对',
},
appFrame:{
显示:“flex”,
flexDirection:'列',
溢出x:'自动',
},
contentWithSidebar:{
显示:“flex”,
flexGrow:1,
},
内容:{
显示:“flex”,
flexDirection:'列',
flexGrow:2,
填充:theme.space.unit*3,
marginTop:“1em”,
paddingLeft:5,
},
});
类MyLayout扩展了组件{
组件willmount(){
this.props.setSidebarVisibility(true);
}
componentDidMount(){
const{kidsLoad,record}=this.props;
kidsLoad({数据:“helloooooo!!!!!!!”})
}
render(){
常数{
儿童
班级,
仪表板
孤岛,
注销,
打开
标题
}=这是道具;
返回(
)
我做错了什么?
如何在此框架中向存储添加数据?这将是一个快速的答案,有待进一步改进
如果我正确理解了您的问题,您的一些资源(实体)已更新,您希望react admin了解这些信息,并相应地更新其存储,必要时触发应用程序视图中的更新
我们首先要得到的是react admin store的dispatch
函数。在我的例子中,资源更新的源是react组件,所以我过去常常收到对dispatch
函数的引用
一旦有了分派的dispatch
功能,例如,react admin store中特定资源更新的CRUD_UPDATE_SUCCESS
操作
import { CRUD_UPDATE_SUCCESS, FETCH_END, UPDATE } from 'react-admin';
dispatch({
type: CRUD_UPDATE_SUCCESS,
payload: { data },
meta: {
resource,
notification: {
body: 'ra.notification.dataSaved',
level: 'info'
},
fetchResponse: UPDATE,
fetchStatus: FETCH_END
}
});
import React from 'react';
import {
CRUD_UPDATE_SUCCESS, FETCH_END, UPDATE, withDataProvider, showNotification
} from 'react-admin';
class Updater extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleUpdate(record) {
const { resource, dataProvider, dispatch } = this.props;
const payload = {
id: record.id,
};
payload.data = {...record};
return new Promise((resolve, reject) => {
dataProvider(UPDATE, resource, payload)
.then(({data}) => {
dispatch({
type: CRUD_UPDATE_SUCCESS,
payload: { data },
meta: {
resource,
notification: {
body: 'ra.notification.dataSaved',
level: 'info'
},
fetchResponse: UPDATE,
fetchStatus: FETCH_END
}
});
resolve(data);
})
.catch(e => {
const errorMessage = e.message || e;
this.setState({ errorMessage });
dispatch(
showNotification(errorMessage, 'warning', { autoHideDuration: 10000 })
);
reject(errorMessage);
});
});
}
render() {
const { record, children } = this.props;
const { errorMessage } = this.state;
return React.Children.only(React.cloneElement(children, {
record,
errorMessage,
onUpdate: this.handleUpdate
}));
}
}
export default withDataProvider(Updater);
您还可以使用react admin.中的操作创建者,例如showNotification
import { showNotification } from 'react-admin';
dispatch(showNotification(errorMessage, 'warning', { autoHideDuration: 10000 }));
这里有一段更为一致的代码来说明所有这些是如何协同工作的。更新程序
组件在这里呈现其子组件,并向它们传递一条资源记录
,然后订阅它们的onSubmit
回调以执行实体保存和更新react admin store
import { CRUD_UPDATE_SUCCESS, FETCH_END, UPDATE } from 'react-admin';
dispatch({
type: CRUD_UPDATE_SUCCESS,
payload: { data },
meta: {
resource,
notification: {
body: 'ra.notification.dataSaved',
level: 'info'
},
fetchResponse: UPDATE,
fetchStatus: FETCH_END
}
});
import React from 'react';
import {
CRUD_UPDATE_SUCCESS, FETCH_END, UPDATE, withDataProvider, showNotification
} from 'react-admin';
class Updater extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleUpdate(record) {
const { resource, dataProvider, dispatch } = this.props;
const payload = {
id: record.id,
};
payload.data = {...record};
return new Promise((resolve, reject) => {
dataProvider(UPDATE, resource, payload)
.then(({data}) => {
dispatch({
type: CRUD_UPDATE_SUCCESS,
payload: { data },
meta: {
resource,
notification: {
body: 'ra.notification.dataSaved',
level: 'info'
},
fetchResponse: UPDATE,
fetchStatus: FETCH_END
}
});
resolve(data);
})
.catch(e => {
const errorMessage = e.message || e;
this.setState({ errorMessage });
dispatch(
showNotification(errorMessage, 'warning', { autoHideDuration: 10000 })
);
reject(errorMessage);
});
});
}
render() {
const { record, children } = this.props;
const { errorMessage } = this.state;
return React.Children.only(React.cloneElement(children, {
record,
errorMessage,
onUpdate: this.handleUpdate
}));
}
}
export default withDataProvider(Updater);
嗯,,
Ed您可以粘贴KidsLoad操作吗?另外,网络选项卡应该显示一个Ajax请求,您可以从中获得有关失败的更多信息。