如何为元素的所有子元素添加样式

时间:2017-08-14 20:27:17

标签: reactjs styled-components

例如,我如何在此代码段中实现addStyles功能

const addStyles = // ...
const Element = ({ children }) => (
    <div>{children.map(addStyles)}</div>
);

感谢

1 个答案:

答案 0 :(得分:2)

使用facebook提供的React.Children.map功能。

  

使用this set to thisArg在子项中包含的每个直接子项上调用一个函数。如果children是一个键控片段或数组,它将被遍历:该函数永远不会传递给容器对象。如果children为null或undefined,则返回null或undefined而不是数组。

然后使用React.cloneElement以适当的样式返回每个孩子的克隆。这将浅显合并第二个参数中对象中提供的任何属性。那些你可以使用它来覆盖孩子们的当前风格。如果您愿意,可以将const StylishComponent = ({ children }) => { const iLikeBlueStyles = { color: "blue" }; const clones = React.Children.map(children, child => { return React.cloneElement(child, { style: iLikeBlueStyles }); }); return ( <div> {clones} </div> ); }; 作为第二个参数提供。

Microsoft.SharePoint.Client = CSOM
ListCollection lists = CSOM.web.Lists;
clientContext.Load(lists);
clientContext.ExecuteQuery();
try
{
   foreach(CSOM.List list in lists
   {
      do.stuff()
   }
}
catch(Excetion ex)
{
   // I get this excetpion in my log at CSOM.List after 'in lists' is read.
   /*System.InvalidCastException: Unable to cast object of type 
    'System.Collections.Generic.Dictionary`2[System.String,System.Object]' 
     to type 'Microsoft.SharePoint.Client.List'.*/
}
相关问题