Closed AleXr64 closed 7 years ago
public class Test : StatelessComponent<Test.Props>
{
public class Props
{ }
public Test(Props props, params Union<ReactElement, string>[] children) : base(props, children) { }
public override ReactElement Render()
{
var result = ReactElementList.Empty;
result.Add(DOM.Div("Test"));
return DOM.Div(result);
}
}
public class App
{
public static void Main()
{
React.Render(new Test(null),
Document.GetElementById("test"));
}
}
Hmm... I'm really doing something wrong, because this code returns div without childs too. How I must use ReactElementList?
ReactElementList
is an immutable list class, so calling the Add
method on it doesn't change the existing list. Instead, it returns a new list with the added item.
So change your code to this: (note the result = result.Add(...)
bit)
public override ReactElement Render()
{
var result = ReactElementList.Empty;
foreach (var item in props.items)
result = result.Add(new TestItem(
new TestItem.Props
{
act = item.ActToGo,
Content = item.TextToShow
}
));
return DOM.Div(result);
}
You can use LINQ instead if you wanted:
public override ReactElement Render()
{
return DOM.Div(
props.items.Select(item => new TestItem(
new TestItem.Props
{
act = item.ActToGo,
Content = item.TextToShow
}
))
);
}
That's right. You need to use the return value of "Add".
This code returns single blank DIV without creating "TestItem" components. What I'm doing wrong?