React stateless component this.refs..value?

后端 未结 7 1693
故里飘歌
故里飘歌 2020-12-03 01:09

I don\'t know if I\'m doing this correctly... If I want to get value from an input I use this.refs.whatever.value.trim() but if that input is a stateless function component

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 01:33

    @inanc, show good method, but I propose an alternative way to use event target to get the DOM element reference. As you are using the form element, you can name your input elements and use it to access the form object.

    const onSubmit = fn => e => {
      e.preventDefault()
      const city = e.target.city.value // Access elements through `form`
      if (city) {
        fn(city)
      }
    }
    
    const MyComponent = ({
      onChange
    }) => {
      return ( 
        
    ) }

提交回复
热议问题