我創建了三個基本組件。React.js傳遞組件之間的數據流
A渲染組件B和C B就像包含選項卡的標題1,2,3 C是第一個有兩種形式的頁面,每次顯示一個。在顯示第一種形式時,我需要在B組件中顯示選項卡1。在顯示第二種形式時,我需要顯示B組件中的選項卡3。
我只想從C組件的數據傳遞到B組件的基礎上。
我把狀態放在C組件上,並嘗試使用相同的this.state.data或this.props.data,因爲B控制器中沒有值。
A.jsx
import React from 'react';
import B from './B.jsx';
import C from './C.jsx'
class A extends React.Component {
constructor(props) {
super();
this.state = {
show : '1',
}
}
render() {
return (
<div>{this.props.show}
<B />
<C/>
</div>
)
}
}
export default A;
B.jsx
import React from 'react';
class B extends React.Component {
constructor(props) {
super(props);
this.state = {
show : '1',
}
}
render() {
return (
//html code here
)
}
}
C.jsx
class C extends React.Component {
constructor(props) {
super(props);
this.state = {
show : '1',
}
}
render() {
return (
//html code here
)
}
}
你可以顯示你的代碼,你試過? –
@MayankShukla我添加了一些代碼來理解。 –