简体   繁体   English

由于执行错误,react createref 返回错误

[英]react createref was returning the error due to wrong implementation

This is the edited question after submitting the answer这是提交答案后编辑的问题

In this code, my file browser will now directly open but when I am submiting the final button then I am not getting the updated state.在这段代码中,我的文件浏览器现在将直接打开,但是当我提交最终按钮时,我没有得到更新的状态。

uploadImage() will be converting the image to base 64 and then update the value on the state. uploadImage() 会将图像转换为 base 64,然后更新状态的值。

uploadCode() will be used to final send the data after clicking on submit button. uploadCode() 将用于在单击提交按钮后最终发送数据。 I have checked that I am not getting the updated value of state in this function according to this logic ie label & htmlFor.我已经检查过我没有根据这个逻辑(即标签和 htmlFor)在这个函数中获得更新的状态值。

My earlier logic was fine when click on upload image div then set the state variable show image from false to true;当点击上传图片 div 然后将状态变量 show image 从 false 设置为 true 时,我之前的逻辑很好; Choose file button only visible when state is true.选择文件按钮仅在状态为真时可见。 Rest all implementation is same and that was working fine.其余所有实现都是相同的,并且工作正常。 But now I am be able to getting the updated state that is why when submit button click I am not getting the image as state is not updated.但是现在我能够获得更新的状态,这就是为什么当单击提交按钮时我没有获得图像,因为状态没有更新。

 constructor(props, context) {
    super(props, context);
    this.inputFile = React.createRef()
    this.onButtonClick = this.onButtonClick.bind(this);
}
uploadImage = (e) => {
    console.log('function called')
/*************New Edit*************/
// getting the image through base64 string and then update it on state;
this.setState({image: myBase64String}, () => { console.log(this.state.image )});
// Return the string through above console
  }

uploadCode = () => {
const {image} = this.state;
console.log(image);//returns undefined;
}
render(){
 return (
    <div><Button onClick={()=>this.uploadCode()} >SUBMIT</Button></div>
      <div className={cx(styles['display-tablecell'], styles['pl-lg'])}>
        <FormControl
          style={{display: 'none'}}
          id="formControlsFile"
          type="file"
          label="File"
          onChange={this.uploadImage}
          name="image"
          ref={this.inputFile}
        />
        <label
          style={{display: 'inline-block'}}
          // onClick={this.onButtonClick}
          htmlFor="formControlsFile" <---- binding to the input tag using the same id
        >
          <i className={cx(fontStyles.fa, fontStyles['fa-image'])} />
        </label>
      </div>
)
}

You are trying to open the file explorer when user clicks on the <span /> .当用户单击<span />时,您正在尝试打开文件资源管理器。 However, you do not need to simulate the click behaviour to achieve that.但是,您不需要模拟click行为来实现这一点。 You can make use of the html <label /> tag to bind the onclick functionality of <input type="file" /> .您可以使用 html <label />标签来绑定<input type="file" />onclick功能。 Here's how -就是这样 -

class App extends Component {
  constructor(props, context) {
    super(props, context)
    /* You won't need these
    this.inputFile = React.createRef()
    this.onButtonClick = this.onButtonClick.bind(this)
    */
  }

  uploadImage = (e) => {
    console.log('function called')
/*************New Edit*************/
// getting the image through base64 string and then update it on state;
this.setState({image: myBase64String}, () => { console.log(this.state.image )});
// Return the string through above console
  }

  /* You won't need these
  onButtonClick = () => {
    console.log('div clicked')
    this.inputFile.current.click()
  }
  */
uploadCode = () => {
const {image} = this.state;
console.log(image);//returns undefined;
}
  render() {
    return (
<div><Button onClick={()=>this.uploadCode()} >SUBMIT</Button></div>
      <div className={cx(styles['display-tablecell'], styles['pl-lg'])}>
        <FormControl
          style={{display: 'none'}}
          id="formControlsFile"
          type="file"
          label="File"
          onChange={this.uploadImage}
          name="image"
          ref={this.inputFile}
        />
        <label
          style={{display: 'inline-block'}}
          // onClick={this.onButtonClick}
          htmlFor="formControlsFile" <---- binding to the input tag using the same id
        >
          <i className={cx(fontStyles.fa, fontStyles['fa-image'])} />
        </label>
      </div>
    )
  }
}

You can find more information about <label /> tag here .您可以在此处找到有关<label />标签的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM