Плагин добавляет выражения в виде React-компонентов, такие, как <For />, <If />, <Switch />.
in
<div>
<Switch value={props.current}>
<Case value="foo">
<If true={props.bar}>
<div> Foo..bar </div>
</If>
</Case>
<Case value={props.name}>
<For each="item" in={props.items}>
<Item key={item.id} content={item.value} />
</For>
</Case>
<Default>
<For in={props.defaultItems} key-is="id">
<Item />
</For>
</Default>
</Switch>
</div>
out
<div>
{function (value, case1, case2) {
switch (value) {
case case1:
return props.bar && <div> Foo..bar </div>;
case case2:
return <span>{Array.prototype.map.call(props.items, function (item, index) {
return <Item key={item.id} content={item.value} />;
}, this)}</span>;
}
return <span>{Array.prototype.map.call(props.defaultItems, function (value, index) {
return <Item key={value.id} {...value} />;
}, this)}</span>;
}.call(this, props.current, "foo", props.name)}
</div>;
npm install --save-dev babel-plugin-transform-react-statements
- in (expression) - iterable object.
- each (string) - variable name for each items of iterable object. Not mandatory.
- counter (string) - the name of index variable. By default, index.
- key-is (string) - the name of the property that stores the unique identifier of the element. Not required.
in
<For in={props.items} key-is="id">
<Item />
</For>
out
<span>{Array.prototype.map.call(props.items, function (value, index) {
return <Item key={value.id} {...value} />;
}, this)}</span>;
- true/false (expression) - condition statement.
<If false={props.hidden}>
<div> Text </div>
</If>
- value (expression) - condition statement.
- <Case value={string | expression} />
- <Default />
<Switch value={props.axle}>
<Case value="x">
<div> X </div>
</Case>
<Case value="y">
<div> Y </div>
</Case>
</Switch>
- props (string) - the name of first attribute. By default, props.
in
<Component props="item">
<div> Item: <span>{item.name}</span> </div>
</Component>
out
item => <div> Item: <span>{item.name}</span> </div>;