Javascript 在FieldArray内触发字段交换时,重新使用旧的/不正确的字段值
我正在为我的项目使用。我正在创建一个旅行应用程序来计算旅行的距离和持续时间。行程可以有一站或多站。可从以下位置选择站点: 从GooglePlaces中选择所需位置后,应用程序将使用计算距离 当用户从下拉列表中选择位置时,但当用户交换停止/字段(使用了Javascript 在FieldArray内触发字段交换时,重新使用旧的/不正确的字段值,javascript,reactjs,redux,react-redux,redux-form,Javascript,Reactjs,Redux,React Redux,Redux Form,我正在为我的项目使用。我正在创建一个旅行应用程序来计算旅行的距离和持续时间。行程可以有一站或多站。可从以下位置选择站点: 从GooglePlaces中选择所需位置后,应用程序将使用计算距离 当用户从下拉列表中选择位置时,但当用户交换停止/字段(使用了字段。交换(indexA:Integer,indexB:Integer)时,我成功地执行了所有计算。显示的结果不正确。在计算方法I内部“调试”值,当用户执行字段交换时,值(索引)根本不会更改,但字段数组更改会正确呈现 此外,我还在redux表单存储中
字段。交换(indexA:Integer,indexB:Integer)
时,我成功地执行了所有计算。显示的结果不正确。在计算方法I内部“调试”值,当用户执行字段交换时,值(索引)根本不会更改,但字段数组更改会正确呈现
此外,我还在redux表单存储中添加新元素,因为我需要提交一些值,即使它们根本没有字段。不确定这是否是一个好方法,所以我愿意听取建议
有人知道问题出在哪里吗
这是我的代码:
价格计算:
updateStopDistanceAndTime = () => {
let self = this;
if (this.props.fields.length >= 2) {
let waypoints = [];
this.props.fields.getAll().map(function (value, index) {
console.log(value);
if (value.place) {
waypoints.push({
place: value.place,
placeId: value.placeId
});
}
});
calculateTimeAndValueBetweenStops(waypoints).then(function (result) {
if (Object.keys(result).length > 0) {
let currentValues;
result.map(function (value, index) {
currentValues = self.props.fields.get(index);
if (value.end_address !== value.start_address) {
currentValues.distance = value.distance;
currentValues.duration = value.duration;
// self.props.changeFieldValue('busRentForm', index + ".distance", value.distance);
// self.props.changeFieldValue('busRentForm', index + ".duration", value.duration);
}
else {
// self.props.changeFieldValue('busRentForm', index + ".distance", null);
// self.props.changeFieldValue('busRentForm', index + ".duration", null);
currentValues.distance = null;
currentValues.duration = null;
}
self.props.fields.remove(index);
self.props.fields.insert(index, currentValues);
});
}
});
}
}
字段数组:
render() {
const {
stop,
index,
dayIndex,
fields,
isDragging,
connectDragSource,
connectDropTarget,
} = this.props
return connectDragSource(connectDropTarget(
<div key={index} className="">
<div>
<div className="col-md-6 col-xs-7">
<Field name={`${stop}.place`}
type="text"
component={renderStopInput}
icon={String.fromCharCode(97 + index)} // (97) is A in ascii table
label={(index === 0) ? "Starting location" : (index === fields.length - 1) ? "Destination location" : "Stop location"}
placeholder=""
index={index}
/>
<span className={index !== fields.length - 1 ? "vertical-dash" : null}></span>
{(index !== 0 && index !== fields.length - 1) ?
<button className="btn btn-fab btn-rearrange" type="button" onClick={() => {
fields.swap(index, index + 1);
}}><i className="fa fa-exchange"></i></button>
:
null}
</div>
<div className="col-md-1 stack">
{(index !== fields.length - 1 && index !== 0) ?
<button className="btn-remove" type="button" onClick={() => fields.remove(index)}>
<span className="icon-Cancel"></span>
</button>
: null}
</div>
{(index !== fields.length - 1 && fields.get(index)) ?
<StopInfo
departureTime={fields.get(index).time }
distance={fields.get(index).distance}
duration={fields.get(index).duration}
/>
: null}
</div>
</div>
));
}
render(){
常数{
停止
指数
日指数,
领域,
是的,
连接DragSource,
连接DropTarget,
}=这是道具
返回connectDragSource(connectDropTarget(
{(索引!==0&&index!==fields.length-1)?
{
字段交换(索引,索引+1);
}}>
:
空}
{(索引!==fields.length-1&&index!==0)?
字段。删除(索引)}>
:null}
{(索引!==fields.length-1&&fields.get(索引))?
:null}
));
}