【发布时间】:2019-06-04 17:44:51
【问题描述】:
我无法从mapDispatchToProps 访问属性,当我使用this.props.updateCar(car) 时它说:
“Readonly &”类型上不存在属性“updateCar” 只读'
这是我的代码:
import * as React from "react";
import { RootState } from "src/reducers";
import { updateCar } from "src/Car/actions";
import { CarState } from "src/Car/reducers";
import { connect } from "react-redux";
const mapStateToProps = (state: RootState) => ({
car: state.car
});
const mapDispatchToProps = (dispatch: any) => ({
updateCar: (car: CarState) => dispatch(updateCar(car))
});
type StateProps = ReturnType<typeof mapStateToProps>;
type MapDispatchToProps = typeof mapDispatchToProps;
type Props = StateProps & MapDispatchToProps;
class CarContainer extends React.Component<Props> {
updateCar = () => {
const car: CarState = {
price: 1,
name: "Mercedes"
};
this.props.updateCar(car);
};
render() {
return (
<div>
{this.props.car.name}
<button onClick={this.updateCar}>UPDATE</button>
</div>
);
}
}
export default connect<StateProps, MapDispatchToProps>(
mapStateToProps,
mapDispatchToProps
)(CarContainer);
【问题讨论】:
标签: reactjs typescript redux react-redux