我有一个父组件(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 = [{…}, {…}, {…}];
PIPIONE
侃侃尔雅
慕慕森
相关分类