2017-07-18 35 views
1

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

这是怎么回事?我期望在我的日志中看到hello world,但是我却看到:

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

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 
} 

回答

1

答案:我不仅将toggleModal传递给anotherFunctionInMyComponent,而且传递给onPress支柱,并使其成为一个综合事件。我忘了onPress道具,所以控制台日志实际上是从那里而不是anotherFunctionInMyComponent。所以在意识到我能够将参数传递给onPress的预期功能后。

1

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

欲了解更多信息,请参阅反应文档Handling EventsSyntheticEvent API reference

现在,看你的例子,我am有点困惑发生了什么事情。基于该片段,是的,我期望A将是字符串“hello world”。如果你能展示更多的代码,可能会有更多的帮助。

+0

我认为你说的是​​某种合成事件,但是'A.target.value'返回undefined,'A.type'返回272.我添加了更多的代码来帮助你查看组件的结构。 – Turnipdabeets

相关问题