【问题标题】:React function parameter is a Proxy?React 函数参数是 Proxy 吗?
【发布时间】:2017-07-18 21:00:09
【问题描述】:

我的有状态 React 组件中有一个函数,我得到的是 Proxy,而不是函数参数中我实际期望的值。

这里发生了什么?我希望在我的日志中看到hello world,但我看到的是:

这是什么?代理{dispatchConfig:对象,_targetInst:构造函数,nativeEvent: 对象,类型:未定义,目标:196…} [[处理程序]]:对象 [[目标]] : ResponderSyntheticEvent [[IsRevoked]] : false

class MyComponent extends Component {

state={}

toggleModal = (A) => {
    console.log('what is this?', A)
    this.setState((prevState, props) => {
      return { openShippingModal: !prevState.openShippingModal };
    });
   }

anotherFunctionInMyComponent = selection => {
    ///do stuff
    this.toggleModal('hello world');
  };

componentDidMount = () => {
 /* do sutff */
}  

render(){
 //return stuff
}

【问题讨论】:

    标签: javascript reactjs react-native proxy redux


    【解决方案1】:

    React 使用“合成事件”系统来平滑不同浏览器中事件处理之间的差异。您通常可以将该参数视为标准浏览器事件对象,即使用A.target.value 或类似的东西。

    有关详细信息,请参阅 Handling Events 和 SyntheticEvent API reference 上的 React 文档。

    现在,看看你的例子,我是有点困惑发生了什么。基于那个 sn-p,是的,我希望 A 将是字符串“hello world”。如果您可以显示更多代码,可能会提供更多帮助。

    【讨论】:

    • 我认为您认为它是某种合成事件是正确的,但是A.target.value 返回 undefined 而A.type 返回 272。我添加了更多代码来帮助您查看组件的结构。
    【解决方案2】:

    答案:我不仅将toggleModal 传递给anotherFunctionInMyComponent,而且还传递给了onPress 道具,导致它成为合成事件。我忘记了 onPress 属性,所以控制台日志实际上来自那里,而不是 anotherFunctionInMyComponent。因此,在意识到我能够按预期从onPress 向函数传递参数之后。

    【讨论】:

      猜你喜欢
      • 2012-09-28
      • 2021-05-15
      • 2018-02-18
      • 1970-01-01
      • 2015-07-21
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多