当混合Redux状态道具时,获取VS代码以正确检测React Props

发布于 2025-01-26 10:30:14 字数 2980 浏览 2 评论 0 原文

有一个 react中的好示例-REDUX文档关于如何构建将从Redux和其父级组件接收prop的功能组件:

这是来自该示例。请注意,我更名为组件mycomponent2,因为我已经有一个叫MyComponent:

import * as React from 'react'
import { connect } from 'react-redux'

interface StateProps {
  isOn: boolean
}

interface DispatchProps {
  toggleOn: () => void
}

interface OwnProps {
  backgroundColor: string
}

type Props = StateProps & DispatchProps & OwnProps

const mapState = (state: StateProps) => ({
  isOn: state.isOn,
})

const mapDispatch = {
  toggleOn: () => ({ type: 'TOGGLE_IS_ON' }),
}

const MyComponent2 = (props: Props) => (
  <div style={{ backgroundColor: props.backgroundColor }}>
    <button onClick={props.toggleOn}>
      Toggle is {props.isOn ? 'ON' : 'OFF'}
    </button>
  </div>
)

// Typical usage: `connect` is called after the component is defined
export default connect<StateProps, DispatchProps, OwnProps>(
  mapState,
  mapDispatch
)(MyComponent2)

但是,当我尝试在另一个组件中使用该组件时,VS代码正在抱怨此组件属性:

​代码还是VS代码变得困惑?无论如何,解决此问题的正确解决方案是什么?

为了完成,这是父组件的代码。请注意,这是一个类组件,我正在寻找一个解决方案,在该解决方案中,孩子组件最终也将是一个类组件:

import * as React from 'react'
import { connect } from 'react-redux'
import MyComponent2 from '../MyComponent2'

interface IMyMainComponentProps {
  name: string,
  color: string
}

interface IMyMainComponentState {
  humor: string
};

const mapStateToProps = (state: IMyMainComponentState) => ({
})

const mapDispatchToProps = {
};

@connect(mapStateToProps, mapDispatchToProps)
export class MyMainComponent extends React.Component<IMyMainComponentProps, IMyMainComponentState> {
  constructor(props?: IMyMainComponentProps) {
    super(props);

    this.state = {
      humor: 'happy'
    }
  }

  render() {
    return(
      <div>
        <h1>{this.props.name}</h1>
        <div>
          {this.props.color}
        </div>
        <div>
          <MyComponent2 backgroundColor='white' />
        </div>
      </div>
    );
  }
}

update:

我让mycomponent2在codesandbox上显示正常:

所以,我真的不知道我的计算机上的VS代码有什么不同:

https://codesandbox.io/s/redux-toolkit-class-class-class-class-class-complas-class-components-forked-forked-forked-js2e2e2e2e2h?file = /src/app.tsx

There is a nice example in the React-Redux documentation on how to build a functional component that will receive both props from Redux and from its parent component:

Here is the code from that example. Mind you, I renamed the component MyComponent2 since I already had one called MyComponent:

import * as React from 'react'
import { connect } from 'react-redux'

interface StateProps {
  isOn: boolean
}

interface DispatchProps {
  toggleOn: () => void
}

interface OwnProps {
  backgroundColor: string
}

type Props = StateProps & DispatchProps & OwnProps

const mapState = (state: StateProps) => ({
  isOn: state.isOn,
})

const mapDispatch = {
  toggleOn: () => ({ type: 'TOGGLE_IS_ON' }),
}

const MyComponent2 = (props: Props) => (
  <div style={{ backgroundColor: props.backgroundColor }}>
    <button onClick={props.toggleOn}>
      Toggle is {props.isOn ? 'ON' : 'OFF'}
    </button>
  </div>
)

// Typical usage: `connect` is called after the component is defined
export default connect<StateProps, DispatchProps, OwnProps>(
  mapState,
  mapDispatch
)(MyComponent2)

However, when I try to use that component in another one, VS Code is complaining about this component properties:

enter image description here

Is anything wrong with the code or is it VS Code getting confused? And in any case, what would be the proper solution to fix this?

For the sake of completion, here is the code of the parent component. Note that it is a class component and that I am looking for a solution where the child component would also eventually be a class component:

import * as React from 'react'
import { connect } from 'react-redux'
import MyComponent2 from '../MyComponent2'

interface IMyMainComponentProps {
  name: string,
  color: string
}

interface IMyMainComponentState {
  humor: string
};

const mapStateToProps = (state: IMyMainComponentState) => ({
})

const mapDispatchToProps = {
};

@connect(mapStateToProps, mapDispatchToProps)
export class MyMainComponent extends React.Component<IMyMainComponentProps, IMyMainComponentState> {
  constructor(props?: IMyMainComponentProps) {
    super(props);

    this.state = {
      humor: 'happy'
    }
  }

  render() {
    return(
      <div>
        <h1>{this.props.name}</h1>
        <div>
          {this.props.color}
        </div>
        <div>
          <MyComponent2 backgroundColor='white' />
        </div>
      </div>
    );
  }
}

UPDATE:

I have MyComponent2 displaying fine on CodeSandbox:

https://codesandbox.io/s/redux-toolkit-class-components-forked-js2e2h?file=/src/App.tsx

So, I really don't know what it different in my VS Code on my computer:

https://codesandbox.io/s/redux-toolkit-class-components-forked-js2e2h?file=/src/App.tsx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文