React js onClick can't pass value to method

后端 未结 30 2138
北荒
北荒 2020-11-22 07:05

I want to read the onClick event value properties. But when I click on it, I see something like this on the console:

SyntheticMouseEvent {dispatchConfig: Ob         


        
30条回答
  •  梦如初夏
    2020-11-22 07:33

    I wrote a wrapper component that can be reused for this purpose that builds on the accepted answers here. If all you need to do is pass a string however, then just add a data-attribute and read it from e.target.dataset (like some others have suggested). By default my wrapper will bind to any prop that is a function and starts with 'on' and automatically pass the data prop back to the caller after all the other event arguments. Although I haven't tested it for performance, it will give you the opportunity to avoid creating the class yourself, and it can be used like this:

    const DataButton = withData('button')
    const DataInput = withData('input');
    

    or for Components and functions

    const DataInput = withData(SomeComponent);
    

    or if you prefer

    const DataButton = withData(

    declare that Outside your container (near your imports)

    Here is usage in a container:

    import withData from './withData';
    const DataInput = withData('input');
    
    export default class Container extends Component {
        state = {
             data: [
                 // ...
             ]
        }
    
        handleItemChange = (e, data) => {
            // here the data is available 
            // ....
        }
    
        render () {
            return (
                
    { this.state.data.map((item, index) => (
    )) }
    ); } }

    Here is the wrapper code 'withData.js:

    import React, { Component } from 'react';
    
    const defaultOptions = {
        events: undefined,
    }
    
    export default (Target, options) => {
        Target = React.isValidElement(Target) ? Target.type : Target;
        options = { ...defaultOptions, ...options }
    
        class WithData extends Component {
            constructor(props, context){
                super(props, context);
                this.handlers = getHandlers(options.events, this);        
            }
    
            render() {
                const { data, children, ...props } = this.props;
                return {children};
            }
    
            static displayName = `withData(${Target.displayName || Target.name || 'Component'})`
        }
    
        return WithData;
    }
    
    function getHandlers(events, thisContext) {
        if(!events)
            events = Object.keys(thisContext.props).filter(prop => prop.startsWith('on') && typeof thisContext.props[prop] === 'function')
        else if (typeof events === 'string')
            events = [events];
    
        return events.reduce((result, eventType) => {
            result[eventType] = (...args) => thisContext.props[eventType](...args, thisContext.props.data);
            return result;
        }, {});
    }
    

提交回复
热议问题