Reactjs: CJSX Nested Conditionals - reactjs

What is the correct syntax to handle nested conditional logic in a React component?
React.createClass
render: ->
<div>
{if #props.showList
{for item in #props.myItems
{item}
}
else
}
</div>
The for loop (on its own) can be rendred; the if/else conditional (on its own) can be rendered. However, nesting the for loop inside the conditional fails.
Any help would be very much appreciated.

I haven't tested it, but based on how normal JSX works you only need the {expression} thing to escape from JSX mode.
The program starts in JS mode (or CS here), and when an element tag is encountered it enters JSX mode. Using {foo} in JSX mode causes it to go back to JS mode. From there the above rules apply, and you can re-enter JSX mode by starting a tag, and so on.
render: ->
<div>
{
if #props.showList
for item in #props.myItems
item
else
<div>{foo}</div>
}
</div>
With annotations:
CoffeeScript:
render: ->
JSX within CoffeeScript
<div>
CoffeeScript within JSX within CoffeeScript
{
if #props.showList
for item in #props.myItems
item
else
JSX within CoffeeScript within JSX within CoffeeScript
<div>
CoffeeScript within JSX within CoffeeScript
{foo}
JSX within CoffeeScript
</div>
JSX within CoffeeScript
}
</div>
CoffeeScript
...

Inside the {} sections you can just use normal Coffeescript/CJSX code, as long as it evaluates to a single expression:
React.createClass
render: ->
<div>
{
if #props.showList
for item in #props.myItems
<span key={item.id}>{item}</span>
else
<span>some other content</span>
}
</div>

Related

React set dangerouslySetInnerHTML as empty

Is it ok to set dangerouslySetInnerHTML as empty?
I am using dangerouslySetInnerHTML to render SVG HTML only if a condition is met. My code:
<div dangerouslySetInnerHTML={ custom && isSVG(custom) && { __html: DOMPurify.sanitize(custom) } }>
</div>
If custom is true and isSVG(custom) is true I am rendering the HTML. Otherwise, dangerouslySetInnerHTML will be set to empty.
The code seems to work but I am curious if this is a correct way to do it. Can setting dangerouslySetInnerHTML to empty cause any issues?

Render open and closing list tags for list items using draft-js and draft-convert

I am using draft-js#0.11.7 and draft-convert#2.2.12
Ive tried the following three methods:
if (block.type === 'ordered-list-item') {
switch (block.depth) {
case 1:
return {
start: `<li className="list-inside list-loweralpha indent-6">`,
end: '</li>',
nest: '<ol>',
nestStart: '<ol>',
nestEnd: '</ol>',
};
case 2:
return (
<ol>
<li className="list-inside list-lowerroman indent-12">{block.text}</li>
</ol>
);
default:
return <li className="list-inside list-decimal" />;
}
}
This is the output:
This is the DOM:
As you can see:
The default (nested length 0) renders the <li> but no <ol> or </ol> which means every subsequent list will continue the numbering order after the last <li> in the DOM. If I had an <ul> before hand it would not start at 1.
The first nesting level renders the <ol> and </ol> tags correctly but removes my styles.
The second nested level renders a <ol><li></li></ol> for each block which means the numbering resets for each block.
I am just trying to mimic the functionality as seen in the DraftJS editor on the left.
If I remove any checking for list blocks in the convertToHTML section my lists are rendered correctly in the dom but are not styled and have no bullet points (list-style-type css property).
I have actually found a solution to this.
Method 1 is the preferred solution to correctly render the DOM elements, but styling only works with style="" and not className="".
The working return for the draft-js block type is:
return {
start: `<li
style="
list-style-position: inside;
list-style-type: lower-alpha;
text-indent: 1.5rem;"
>`,
end: '</li>',
nest: '<ol>',
nestStart: '<ol>',
nestEnd: '</ol>',
};
The Lists are now rendering as expected:

My isotope (jQuery plugin) is not filtering when the identifying class is passed props in ReactJS

My isotope filtering is working perfectly when the identifying class is hard-coded in the grid elements, but it fails to function when the elements are rendered using jQuery Ajax call in ReactJS, and the identifying class are set using props passed to it by parent component.
When I hard-code the className="block hvr-reveal CategoryA " , then it seems to work fine, but when I pass the same through props, it doesn't.
Also it's getting displayed if I append a <p> tag right after the <img> tag and display {this.props.category}.
var Template = React.createClass({
render: function() {
return (
<div className="container text-center">
<div className="block hvr-reveal {this.props.category} " data-category={this.props.category} >
<img src={this.props.url}></img>
</div>
</div>
);
}
});

React Component with subcomponents or render HTML in parent?

I have a question regarding React patterns. When rendering a component, is it appropriate for this component to render several sub-components, or is it ok to render some HTML in the parent component. Example:
If I have a box that has a heading and a body with list of elements, should I do:
var Box = React.createClass({
render: function() {
<div className="box">
<HeadingBox />
<BodyBox />
</div>
}
});
Or is it ok to do this:
var Box = React.createClass({
render: function() {
<div className="box">
<div className="heading">
<div> Heading1 </div>
<div> Heading2 </div>
</div>
<BodyBox />
</div>
}
});
Any rules to follow here?
It all depends on a context.
The general practice is that if you want to reuse the markup anywhere — you should go with the separate component, so you don't have to repeat yourself. Also if you find yourself writing a large portion of HTML (over 50 lines, for example), separating it into subcomponents will also help.
In other cases, just going with plain HTML will do.
You can find a good description on how best to organize your React code here. (section Separating UI details from interaction logic)
React is no different then other programming framework — it goes best with DRY (Don't repeat yourself).

Angular JS use NG-Hide with function

I try to create an Angular JS function that is displaying or hiding a Div in case that a certain requirement is met.
I do have the problem now that the function is not properly called and both divs are either visible or not visible (In the test case div 1 should be shown and div 2 not).
testApp.controller('MyController', ['$scope','$http',
function ($scope,$http) {
$scope.checkValue = function(value){
if(value >= 1)
return true;
else
return false;
};
}]);
In the html file I try to hide the Divs using the following parameters
<div class="classa" ng-hide="requestsExisting({{profile.arrayA.length}})">
<div class="classb" ng-hide="requestsExisting({{profile.arrayB.length}})">
Is during the run time the {{profile.parameterA.length}}passed to the function or the actual value that is stored in this variables? (It's 1 for arrayA and 0 for ArrayB)
you don't need the "{{" sign.
just do
<div class="classa" ng-hide="requestsExisting(profile.arrayA.length)">
<div class="classb" ng-hide="requestsExisting(profile.arrayB.length)">
the double curly brace is to put the value of the object in the html
The double curly brace notation {{ }} to bind expressions to elements
is built-in Angular markup
I think that it should work just with this code
<div class="classa" ng-hide="requestsExisting(profile.arrayA.length)">
<div class="classb" ng-hide="requestsExisting(profile.arrayB.length)">
I think that you don't need to use {{}} inside the ng-hide directive

Resources