具有onChange处理程序的无状态React组件:合成事件警告

时间:2016-12-06 13:01:06

标签: javascript reactjs

我正在尝试对状态由无状态Child管理的有状态组件执行简单实现。目前,处理程序仅触发console.log。

预期行为 更新字段时,父组件应触发console.log。

实际行为 setInterest永远不会被触发,而是我收到有关合成事件的错误:

This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the property `nativeEvent` on a released/nullified synthetic event. This is set to null. If you must keep the original synthetic event around, use event.persist().

组件按预期可视化呈现,并且在Webpack的浏览器中我没有得到任何其他错误。

非常感谢任何帮助。

有状态组件:

// Setting the parameters of the market
export class Parameters extends React.Component {
    // Constructor setting default state
    constructor ( props ) {
        super ( props )
        // Set the state objects
        this.state = {
            interest: {
                pessimistic: this.props.pessimistic || 1,
                historical: this.props.historical || 4,
                optimistic: this.props.optimistic || 7
            }
        }
        // Bind the functions for use in the render
        this.setInterest = this.setState.bind( this )
    }

    // Set the parameters
    setInterest( e ) {
        console.log('I AM NEVER TRIGGERED')
    }

    // Rendering of message
    render( ) {
        return(
            <div>
                <ParametersView
                    handleChange={ this.setInterest }
                    interest={ this.state.interest } />
                <DesiresView />
            </div>
        )

    }
}

无状态组件

// Import react
import React from 'react'

export const ParametersView = ( { handleChange, interest }  ) => {
    return (
            <div>
                <span id="assumptions">
                    <input
                        onChange={ handleChange }
                        value={interest.pessimistic}
                        id="pessimistic" type="number" name="pessimistic" />
                    <input
                        onChange={ handleChange }
                        value={interest.historical}
                        id="historical" type="number" name="historical" />
                    <input
                        onChange={ handleChange }
                        value={interest.optimistic}
                        id="optimistic" type="number" name="optimistic" />
                </span>
            </div>
        )
}

export const DesiresView = (  ) => {
    return ( <p>No desire view yet</p> )
}

1 个答案:

答案 0 :(得分:5)

你有一个错字。

this.setInterest = this.setState.bind( this )需要

this.setInterest = this.setInterest.bind( this )