Code Monkey home page Code Monkey logo

Comments (8)

Ryqsky avatar Ryqsky commented on May 13, 2024

是小程序场景下 onChange 不能响应,但如果使用 addEventListener 添加事件是可以响应的

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }


  componentDidMount() {
    this.myRef.current.addEventListener('change', onChange)
  }

  render() {
    return <label>
      <input
        ref={this.myRef}
        type="checkbox"
        onChange={onChange}
      />
      这是 checkbox 组件 addEventListener
    </label>;
  }
}

from kbone.

Ryqsky avatar Ryqsky commented on May 13, 2024

尝试修改一下 miniprogram-render/event/event-target.js
178行就可以响应了,但是这种办法太挫了,暂时没有深入 react 事件机制去看

    $_getHandlers(eventName, isCapture, isInit) {
        const handlerMap = this.$_eventHandlerMap

        if (isInit) {
            const handlerObj = handlerMap[eventName] = handlerMap[eventName] || {}

            handlerObj.capture = handlerObj.capture || []
            handlerObj.bubble = handlerObj.bubble || []

            return isCapture ? handlerObj.capture : handlerObj.bubble
        } else {
            let handlers = []

            const handlerObj = handlerMap[eventName]
            if (handlerObj) {
                handlers = handlers.concat(isCapture ? handlerObj.capture : handlerObj.bubble)
            }

            for (let key in this) {
                if (this.hasOwnProperty(key) && /^__reactEventHandlers/.test(key)) {
                    const reactEventHandlers = this[key]
                    Object.keys(reactEventHandlers).map(reactEventName => {
                        if (reactEventName.toLowerCase() === `on${eventName}`) {
                            const targetMethod = reactEventHandlers[reactEventName]
                            if (typeof targetMethod === 'function') {
                                handlers.push(targetMethod)
                            }
                        }
                    })
                }
            }
            return handlers
        }
    }

from kbone.

dntzhang avatar dntzhang commented on May 13, 2024
<input
      type="checkbox"
      value="aaa"
      onClick={onClick}
    />
    这是 checkbox 组件
  </label>
function onClick(evt){
  console.log('onClick', evt.currentTarget.checked)
}

直接绑定 onclick

from kbone.

dntzhang avatar dntzhang commented on May 13, 2024

看面看怎么兼容下 onchange, preact 不会出现这个问题

from kbone.

dntzhang avatar dntzhang commented on May 13, 2024

试试 jsx-native-events

/** @jsx nativeEvents */
import nativeEvents from 'jsx-native-events'
import { useState } from 'react'
import 'omim/icon-button'

export default function SomeComponent(props) {
    const [result, setSwitch] = useState(false)

    return (
      <div>
        <p>The switch is {result ? 'on' : 'off'}</p>
        <m-icon-button color="red" icons="['favorite', 'favorite_border']" onEventChange={e => setSwitch(e.detail.isOn)}>
        </m-icon-button>
      </div>
    )
}

from kbone.

Ryqsky avatar Ryqsky commented on May 13, 2024

使用 jsx-native-events 在 jsx 绑定 onChange事件,是可以响应的

from kbone.

dntzhang avatar dntzhang commented on May 13, 2024

使用 jsx-native-events 在 jsx 绑定 onChange事件,是可以响应的

虽然这个库可以解决个性化事件问题,
但是有个问题,就是react组件库原来是用 onChange,无法修改组件库的代码,这个暂时没想到怎么解决

-- by easonruan

from kbone.

JuneAndGreen avatar JuneAndGreen commented on May 13, 2024

已在 [email protected][email protected] 兼容修复

同时也补充了 demo:https://github.com/wechat-miniprogram/kbone/tree/develop/examples/demo16

from kbone.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.