【问题标题】:React Redux dispatch on componentDidMount -> property does not exist在 componentDidMount -> 属性上的 React Redux 调度不存在
【发布时间】:2020-02-01 11:07:12
【问题描述】:

我正在使用 React + Redux 处理我的第一个项目,当我尝试在 componentDidMount 部分中调度一个函数时遇到了一些问题。我尝试关注文档中的 Reddit API example 项目,但由于他们使用 JavaScript 而我使用的是 TypeScript,所以并非所有内容都相同。

这是我试图实现这一目标的 React 组件:

export class EducationComponent extends Component {
    constructor(props: any) {
        super(props);
    }

    componentDidMount() {
        const { dispatch, getUser } = this.props;
        dispatch(getUser());
    }
    public render() {
        return (
            <Content className="component">
                <Demo/>
            </Content>
        );
    }
}

function mapStateToProps(state: State) {
    const { isLoaded, isFetching, user } = state.userProfile;
    return {
        user,
        isFetching,
        isLoaded
    }
}

export default connect(mapStateToProps)(EducationComponent)
export const Education = (EducationComponent);

我在const { dispatch, getUser } = this.props; 行中收到以下错误:

Error:(16, 17) TS2339: Property 'dispatch' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'.
Error:(16, 27) TS2339: Property 'getUser' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'.

如果有任何不确定,可以在这里找到该项目:https://github.com/jLemmings/GoCVFrontend

这是完成这项工作的正确方法还是有更好的选择?

谢谢

编辑当前状态:

const {Content} = Layout;

export class EducationComponent extends Component {
    constructor(props: any) {
        super(props);
    }

    componentDidMount() {
        this.props.userAction();
    }
    public render() {
        return (
            <Content className="component">
                <Demo/>
            </Content>
        );
    }
}

const mapDispatchToProps = (dispatch: Dispatch) => bindActionCreators({
    userAction: action.getUser,
}, dispatch);

function mapStateToProps(state: State) {
    const { isLoaded, isFetching, user } = state.userProfile;
    return {
        user,
        isFetching,
        isLoaded
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(EducationComponent)

最终编辑(让它工作):

interface MyProps {
    getUser: () => void
}

interface MyState {
    userAction: ThunkAction<Promise<void>, {}, {}, AnyAction>
}

export class EducationComponent extends Component<MyProps, MyState> {
    static defaultProps = {getUser: undefined};

    constructor(props: any) {
        super(props);
    }

    componentDidMount() {
        this.props.getUser()
    }

    public render() {
        return (
            <Content className="component">
                <Demo/>
            </Content>
        );
    }

}

const mapDispatchToProps = (dispatch: ThunkDispatch<{}, {}, any>, ownProps: MyProps) => {
    return {
        getUser: () => {
            dispatch(getUser());
        }
    }
}

function mapStateToProps(state: State) {
    const {isLoaded, isFetching, user} = state.userProfile;
    return {
        user,
        isFetching,
        isLoaded
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(EducationComponent)

【问题讨论】:

标签: reactjs typescript redux react-redux


【解决方案1】:

你可以这样做

componentDidMount() {
   this.props.getUser();
}

// mapStateToProps

mapDispatchToProps = (state) => {
   getUser: () => dispatch(getUser()); //Don't forget to import getUser from your action creator
}

export default connect(mapStateToProps, mapDispatchToProps)(EducationComponent)
export const Education = (EducationComponent); // Delete this line no need for this because you're already exporting it as default

参考:https://react-redux.js.org/using-react-redux/connect-mapdispatch#arguments

【讨论】:

  • 感谢您的建议,我已经添加了代码的当前状态,因为它仍然无法正常工作。我只是无法访问道具中的 userAction...
  • 您可以尝试像这样const mapDispatchToProps = dispatch =&gt; { return { // dispatching actions returned by action creators increment: () =&gt; dispatch(increment()), decrement: () =&gt; dispatch(decrement()), reset: () =&gt; dispatch(reset()) } } 编写您的 mapDispatchToProps
  • 感谢您的帮助,但没有成功。我正在使用 TypeScript 而不是 JavaScript,所以要遵循您的指导方针有点困难......
  • @In0cenT 这可能会解决它stackoverflow.com/questions/47561848/…,因为 TS 不知道调度的类型
【解决方案2】:

你是否使用mapDispatchToProps绑定调度方法?

【讨论】:

    【解决方案3】:

    查看您的项目后,您正在像这样导入组件:

    import {Education} from "../pages/Public/Education";
    

    这将导入未连接的组件,这就是您无法访问调度的原因。

    你应该导入默认的连接组件:

    import Education from "../pages/Public/Education";
    

    【讨论】:

    • 感谢您的建议,我已经添加了代码的当前状态,因为它仍然无法正常工作。我只是无法访问道具中的 userAction...
    • 你是在做import {EducationComponent},还是import EducationComponent
    • 对不起,我应该提到我按照您的建议导入它:import EducationComponent
    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 2017-09-17
    • 2018-12-17
    • 1970-01-01
    • 2019-06-18
    • 2016-04-11
    • 2017-07-28
    • 1970-01-01
    相关资源
    最近更新 更多