reactjs Redux connect的mapStateToProps的正确类型声明(TypeScript 2)

lpwwtiir  于 2023-05-17  发布在  React
关注(0)|答案(3)|浏览(143)

我有一个Redux商店,想连接它。下面是我的容器组件的摘录:

interface IProps  {
  states: IAppState;
  actions: IAppProps;
}
// mapping state to the props
const mapStateToProps = ( state: IAppState, ownProps = {} ) => ({
  states: state
});
// mapping actions to the props
const mapDispatchToProps = ( dispatch: Redux.Dispatch<IAppState> ) => ({
  actions: Redux.bindActionCreators( actions, dispatch )
});

// connect store to App
@connect<IAppState, IAppProps, any>(
  mapStateToProps,
  mapDispatchToProps
)
export default class App extends React.Component<IProps, {}> {
//...
}

编译时,我收到以下问题:

error TS2345: Argument of type '(state: IAppState, ownProps?: {}) => { states: IAppState; }' is not assignable to parameter of type 'MapStateToPropsParam<IAppState, any>'.
  Type '(state: IAppState, ownProps?: {}) => { states: IAppState; }' is not assignable to type 'MapStateToPropsFactory<IAppState, any>'.
    Type '{ states: IAppState; }' is not assignable to type 'MapStateToProps<IAppState, any>'.
      Type '{ states: IAppState; }' provides no match for the signature '(state: any, ownProps?: any): IAppState'.

我使用的是@types/react-redux@4.4.44,它公开了MapStateToProps接口。我会认为我的mapStateToProps面对这个接口...但有些地方不对劲。知道那是什么吗

8gsdolmq

8gsdolmq1#

好吧,似乎@types/react-redux接受mapStateToProps返回与接收的类型完全相同的类型。我希望它在Map过程中修改它的 prop ,如{ states: AppStateTree }。相反,我修改了状态树combineReducers({ states: myReducer })。这段代码运行良好:

interface IRootState {
  state: IAppState;
}

const mapStateToProps = ( state: IRootState ) => state;

const mapDispatchToProps = {
  toggleOpenAddFeed
};

type IProps = IRootState & typeof mapDispatchToProps;

class App extends React.Component<IProps, {}> {
 render() {
    return (
      <div className="main-wrapper">
         <Mycomponent store={this.props} />
      </div>
    );
  }
}

export default connect( mapStateToProps, mapDispatchToProps )( App );
bpsygsoo

bpsygsoo2#

ownProps没有任何类型。你必须给予它一个类型。

const mapStateToProps = ( state: IAppState, ownProps: any = {} )
5t7ly7z5

5t7ly7z53#

这就是我的工作:

type CounterProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>

class Counter extends Component<CounterProps> {
    toggleCounterHandler() {
    }

    incrementHandler() {
        this.props.increment()
    }

    decrementHandler() {
        this.props.decrement()
    }

    render() {
        return (
            <main className={classes.counter}>
                <h1>Redux Counter</h1>
                <div className={classes.value}>{this.props.counter}</div>
                <div>
                    <button onClick={this.incrementHandler.bind(this)}>Increment</button>
                    <button onClick={this.decrementHandler.bind(this)}>Decrement</button>
                </div>
                <button onClick={this.toggleCounterHandler}>Toggle Counter</button>
            </main>
        );
    }
}

function mapStateToProps(state: AppState) {
    return {
        counter: state.counter
    }
}

function mapDispatchToProps(dispatch: Dispatch<AppAction>) {
    return {
        increment: () => dispatch({ type: 'increment' }),
        decrement: () => dispatch({ type: 'decrement' })
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(Counter)

相关问题