[英]Passing props/this to components (undefined ?) React
I am having an issue being able to pass a value from a parent to child functional component.我在将值从父功能组件传递给子功能组件时遇到问题。
The issue is that this.state.dataURL is undefined when passed to the child but i cant work out why.问题是 this.state.dataURL 在传递给孩子时未定义,但我不知道为什么。
I've tried console logging props in the constructor and that is also undefined我已经在构造函数中尝试过控制台日志记录道具,这也是未定义的
Parent:家长:
import UploadButton from './Components/UploadButton';
import previewTable from './Components/PreviewTable';
default export class uploadDataPage extends React.Component {
constructor(props) {
super(props);
this.state = { dataURL: 'default value' };
this.changeData = this.changeData.bind(this);
}
changeData(newValue) {
this.setState({dataURL : newValue});
}
render() {
return (
<UploadButton dataURL = {this.state.dataURL} onChange={this.changeData}/>
)
}
}
Component零件
import React, { useState} from 'react';
import axios from "axios";
export default function UploadButton ({dataURL}) {
console.log(dataURL);
const uploadHandler = (event) => {
const uploadedFile = new FormData();
uploadedFile.append( 'file',
event.target.files[0],
);
axios.post("http://127.0.0.1:5000/uploadFile", uploadedFile).then(response =>
dataURL.onChange(response.data)
)
}
return(
<div>
<input type="file" name="file" onChange={uploadHandler}/>
</div>
);
};
Please use uppercase for component names: UploadDataPage请使用大写的组件名称:UploadDataPage
Once that's done, I can log dataURL in this sandbox: https://codesandbox.io/s/sharp-star-pemh2?file=/src/App.js:524-538完成后,我可以在此沙箱中记录 dataURL: https://codesandbox.io/s/sharp-star-pemh2?file=/src/App.js:524-538
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.