我正在使用 Reactjs 处理一个表单,该表单从父组件获取一些 defaultValues。
问题是,父组件使用 axios 帖子设置值的状态,并将这些值作为道具传递给子组件。我可以使用 console.log 在子组件上打印这些值,但是如果我尝试将这些值放在 TextFields 上的 defaultValues 上,我会得到一个空表单,表单上没有任何值呈现。
父组件:
export default class Parent extends Component {
constructor(props){
super(props);
this.state={
somevalue: '',
}
}
componentDidMount(){
this.getData();
}
getData = async () => {
await api.post('/getValue')
.then((res) => {
this.setState({
someValue: res.data;
})
}).catch((err) => {
console.log("Error: ", err);
})
}
render(){
return(
<Child value={this.state.someValue}/>
)}
}
Run Code Online (Sandbox Code Playgroud)
子组件
export default function Child(props) {
console.log(props.value); // THIS LOG PRINT THE VALUE PROPERLY
return(
<TextField defaultValue={props.value}/>
)
}
Run Code Online (Sandbox Code Playgroud)
这基本上是我的代码结构,但它不起作用。在此之后,TextField …