2017-03-07 95 views
0

我有一个HeadingComponent其示出了H 2标签像这样内页标题:通道具反应

<div id="id1"> 
     <h2 className="class1">{headingText}</h2> 
</div> 

HeadingComponent是具有其他组件和嵌入的div父DIV中。 ComponentThatDecidesHeading1,ComponentThatDecidesHeading2,ComponentThatDecidesHeading3是将决定应该是什么{headingText}即

<div id="layoutContentArea"> 
       <HeadingComponent headingText={headingText}/> 
       <div or some wrapper component> 
        <ComponentThatDecidesHeading1/> 
        OR 
        <ComponentThatDecidesHeading2/> 
        OR 
        <ComponentThatDecidesHeading3/> 
        </div> 
      </div> 

所以,如果ComponentThatDecidesHeading1呈现,headingText = '标题1',如果ComponentThatDecidesHeading2被渲染的成分,headingText ='标题2'等。

有没有办法把一个“如果”的条件或东西,检查哪个组件被呈现,并根据显示相应的headingText? 或 将headingText从,并传入。

我检查了ReactJS Two components communicating,Pass props to parent component in React.js,但没有得到我的答案。

任何想法?

+0

谁选择渲染哪个标题?它是由用户完成的吗? – Dhiraj

+0

为什么不制作组件 - “ComponentThatDecidesHeading”并根据需要传递道具以确定组件/标题? –

+0

@ Dhiraj-存在组件“ComponentThatDecidesHeading1”,“ComponentThatDecidesHeading2”等决定标题。正如我所提到的,如果当前呈现的组件是“ComponentThatDecidesHeading1”,那么标题将是“标题1”,如此等等。 “HeadingComponent”是在其“h2”标签内显示标题的组件。 – abhi

回答

0

您可以在render()函数中创建一个函数,以在设置标题文本的同时有条件地渲染所需组件。我假设你有一个“ComponentThatDecidesHeading”组件,它从别处接收不同的道具。如果是这样的话,像这样的工作(EDITED回复您的查询):

// Elsewhere in your app: 
 

 
const propsForMyHeadingComponents = [ 
 
{ 
 
    id: 1, 
 
    headingText: 'HeadingText 1', 
 
    data: [thing1, thing2...] 
 
}, 
 
{ 
 
    id: 2, 
 
    headingText: 'HeadingText 2', 
 
    data: [otherThing1, otherThing2, ...] 
 
}, 
 
etc... 
 
]; 
 

 
// Meanwhile, in your parent component: 
 

 
grabDataAndHeadingText(){ 
 
    let headingText; 
 
    let component; 
 

 
    // Assuming the info you need to load into your components is passed in 
 
    // from a parent or from a store like Redux: 
 
    const { propsForMyHeadingComponents } = this.props; 
 

 
    // From there, you can iterate through your array to select the data for the component you want: 
 
    propsForMyHeadingComponents.forEach(thisHeader => { 
 
    if(condition-to-determine-which-component-to-load === thisHeader.id){ 
 
     headingText = thisHeader.headingText; 
 
     data = thisHeader.data; 
 
    }); 
 
    
 
    return { data, headingText }; 
 
} 
 

 
render(){ 
 

 
    const { data, headingText } = this.grabDataAndHeadingText(); 
 

 
    // Again, assuming "ComponentThatDecidesHeading" is the same component 
 
    // with changing props: 
 

 
    return(
 
    <div id="layoutContentArea"> 
 
     <HeadingComponent headingText={ headingText }/> 
 
     <div or some wrapper component> 
 
      <ComponentThatDecidesHeading data={ data } /> 
 
     </div> 
 
    <div> 
 
); 
 
}

你总是可以得到&集headingText和组件变量状态,也是如此。

如果你真的有100个独立的组件,每个组件都有自己的一组独特的逻辑,等等,你可以将组件的名称存储在你的“propsForMyHeadingComponents”数组中。否则,只需使用上面列出的要用其呈现的道具加载组件即可。

+0

谢谢Nate。是的,我将会有很多决定标题的组件(可能是100个)。你能分享另一种方法吗? – abhi

+0

@abhi请参阅上文。 –

+1

它工作。就像你说的,我所有“决定标题”的组件都是彼此不同的(有自己独特的逻辑等)。所以,我稍微修改了一下你的方法。 常量compWithNextPage = [ { 当前页: 'ABC', 下一页: 'XYZ', headingText: 'headin1', 的BodyContent: }, { 当前页:“DEF, 下一页: 'GHI', headingText: 'heading2', 的BodyContent: }, ] – abhi