我是React(和Redux)的新用户,并尝试创建一个简单的表单来创建 Item (name, type
),并重定向创建后的 ListIems 页面。
我正在使用react-thunk来处理异步操作,一切都在打字稿中。
我的问题是如何处理创建结果和重定向?由于我的操作CreateItemRequest
是异步的,如何通知我的容器项目已成功创建,因此它会重定向到 ListIems 页面。
我试过在我的CreateItemSuccess
动作中简单地使用一个布尔值我的reducer,但是,我不能去创建表单本身,因为布尔现在是真的是模型,然后立即重定向到 ListIems 页面。
我被困在这里,我确定我错过了一些愚蠢的东西,但在任何地方找不到任何东西......
import { Route, Redirect } from "react-router-dom";
import { connect, Dispatch } from 'react-redux';
import * as React from 'react';
import * as Types from '../../types';
import * as Actions from '../../actions';
interface ICreateModelDispatchProps {
LoadItems: () => void;
ChangeForm: (model: Actions.IItemCreateModel) => void;
CreateItem: (model: Actions.IItemCreateModel) => void;
}
interface ICreateItemStateProps {
Types: Types.ITypeEntity[];
ItemCreated: boolean;
Model: Actions.IItemCreateModel;
}
class CreateItem extends React.Component<ICreateItemStateProps & ICreateModelDispatchProps, Types.IAppState> {
public componentDidMount() {
this.props.LoadItems();
}
public render() {
if (this.props.ItemCreated) {
const redirect = () => <Redirect to={{ pathname: '/items' }} />;
return <Route render={redirect} />;
}
if (!this.props.Types.length) {
return <div id="create-item" className="loading" />;
}
const { Name, Type } = this.props.Model;
const onSubmit = this.onSubmit.bind(this);
const onNameChange = this.onNameChange.bind(this);
const onTypeChange = this.onTypeChange.bind(this);
return (
<div id="create-item">
<form onSubmit={onSubmit}>
<div>
<label htmlFor="name">Name</label>
<input type="text" id="name" value={Name} placeholder="Item's Name" onChange={onNameChange} />
</div>
<div>
<label htmlFor="type">Type</label>
<select id="type" onChange={onTypeChange} value={Type}>
<option value="-1">Choose a type</option>
{this.props.Types.map(type => <option key={type.Id} value={type.Id}>{type.Name}</option>)}
</select>
</div>
<div>
<input type="submit" value="CREATE" />
</div>
</form>
</div>
)
}
private onNameChange(event: React.ChangeEvent<HTMLInputElement>) {
this.props.ChangeForm(Object.assign(this.props.Model, { Name: event.target.value }));
}
private onTypeChange(event: React.ChangeEvent<HTMLSelectElement>) {
this.props.ChangeForm(Object.assign(this.props.Model, { Type: event.target.value }));
}
private onSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
this.props.CreateItem(this.props.Model);
}
}
export function mapStateToProps(state: Types.IAppState): ICreateItemStateProps {
const types = Object.keys(state.Entities.Types).map(key => state.Entities.Types[Number(key)]);
const Model = state.CreateItem;
return {
Model,
Types: types,
ItemCreated: !!state.CreateItem.Created,
};
}
export function mapDispatchToProps(dispatch: Dispatch): ICreateModelDispatchProps {
return {
LoadItems: () => dispatch(Actions.ITEM_TYPES_REQUEST()),
ChangeForm: (model) => dispatch(Actions.ITEM_CREATE_CHANGE(model)),
CreateItem: (model) => dispatch(Actions.ITEM_CREATE_REQUEST(model)),
};
}
export default connect(mapStateToProps, mapDispatchToProps)(CreateItem);
答案 0 :(得分:3)
您可以使用Promise
或async/await
撰写您的操作。这是js(而不是ts)
export default function ITEM_CREATE_REQUEST(model) {
return (dispatch) => {
return new Promise((resolve, reject) => {
/* SOME STUFF */
resolve(SOME_DATA);
});
}
};
比您在解决时可以使用它进行导航
onSubmit() {
event.preventDefault();
this.props.CreateItem(this.props.Model).then((SOME_DATA) => {
/* PERFORM REDIRECT */
});
}
答案 1 :(得分:2)
当您发送异步操作时,您将在某个不可预测的时间更新商店的状态。您的组件必须通过mapStateToProps
以某种方式接收这些更新。
通过这种方式,你可以处理创建新项目的道具得到更新 - 大概在getSnapshotBeforeUpdate
生命周期内最好:
getSnapshotBeforeUpdate(prevProps, prevState) {
if (prevProps.itemCreated !== this.props.itemCreated) {
// itemCreated could be a counter or something else.
// For example, you count up after item is created.
// Whenever that prop changes, you navigate away like this.
const { history } = this.props
history.pushState(null, '/anywhere') or
history.replaceState(null, '/anywhere')
}
return null;
}
getSnapshotBeforeUpdate
的