【发布时间】:2019-09-18 07:16:29
【问题描述】:
我有一个父组件(index.js),它的状态为x:[],状态包含数据[{…}, {…}, {…}],我有一个子组件(child.jsx),在我想要的子组件(child.jsx)中将父组件数据[{…}, {…}, {…}] 保存在子组件的变量中。
父组件(index.js)
//here i have more imports
import Child from "./child"
export default class Index extends Component {
constructor(props) {
super(props);
this.state = {
x: [],
};
}
//some functions
render() {
const { x } = this.state;
console.log(x, "this is the data")
// x contains data [{…}, {…}, {…}]
return (
<div className="class">
<Autocomplete x={this.state.x} />
</div>
}
}
子组件(child.jsx)
//here i have imports
const suggestions = here i want x data from the parent component;
//some functions
export default function Child(props) {
return (
<div className="material">
<div className={classes.root}
<Autosuggest
{...props.x}
/>
</div>
</div>
);
}
主要是在我尝试传递一些道具时出现未定义的错误。
预期结果:
"x data from the parent component"
const suggestions = [{…}, {…}, {…}];
【问题讨论】:
-
您在 Parent 中导入 Child 但从不对其执行任何操作,是否缺少某些代码?
-
不是
<Autocomplete x={this.state.x} />,你的意思是<Child x={this.state.x} />。 -
不,这是material.ui组件。
标签: javascript reactjs material-ui