我有一個名爲separatefile.jsx的文件,在此文件中,父組件的名稱是Content,子組件的名稱是Child。Reactjs:是否可以使用另一個父組件的父組件屬性
separatefile.jsx
import React from 'react';
import Parent from './learning.jsx';
class Content extends React.Component {
constructor() {
super();
this.state = {
finding : 'i am finding'
}
}
render() {
return (
<div>
<Child childprop={this.state.finding}/>
<Parent/>
</div>
);
}
}
class Child extends React.Component {
render() {
return (
<div>
<h2>{this.props.childprop}</h2>
<h1>child class property</h1>
</div>
);
}
}
export default Content;
這是命名爲learning.jsx另一個文件,這個文件有一個名爲作爲命名爲兒童父母與子女組件父組件。
我的問題是,我需要從子組件(separatefile.jsx檔子組件)訪問父組件屬性(learning.jsx父組件)...
learning.jsx
import React from 'react';
class Parent extends React.Component {
constructor() {
super();
this.state = {
searching : 'i will find the solution'
}
}
render() {
return (
<div>
<Children childrenprop={this.state.searching}/>
</div>
);
}
}
class Children extends React.Component {
render() {
return (
<div>
<h2>{this.props.childrenprop}</h2>
</div>
);
}
}
export default Parent;
高興地TimoSta,再一次簡單地解釋我的問題。我需要從兒童組件訪問'發現'屬性..我希望你能理解 – praveenkumar
它不起作用 – praveenkumar
@PraveenKumarInvoscape我改變了'Parent'的實例化。我犯了一個錯誤,並通過了錯誤的組件。請再試一次嗎? – Timo