0

I don't want to display "no file selected" on input file and replacing with another button.

var form = React.createClass({
   render : function(){
        var fileStyle = { display : none };
        return (
         <form>
            <input id="file" type="file" style={fileStyle}>
            <button onClick={ /* how to perform click on file*/ }> click to add image</button>
         </form>
        );
    }
});

I want when I click button then input file is perform click.

How to accomplish this within React js?

1
  • 1
    Your question is not clear. Commented Sep 26, 2016 at 11:44

2 Answers 2

1
  • Assign a ref to a field
  • Find corresponding dom node using findDOMNode and the ref
  • Trigger click manually.

Demo.

const { Component } = React
const { render, findDOMNode } = ReactDOM

class App extends Component {
  constructor(props) {
    super(props)
    this.trigger = this.trigger.bind(this)
  }

  trigger() {
    findDOMNode(this.file).click()
  }

  render() {
    return (
      <div>
        <input type="file" ref={file => this.file = file} style={{
            display: 'none'
          }} />
        <button type="button" onClick={this.trigger}>Click Me</button>
      </div>
    )
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here is how to include javascript statements in JSX

import React from 'react';

const person = () => {
    return <p>I have {Math.floor(Math.random()*30)} years of experience.</p>
}

export default person;

This example shows a React component using function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.