1. Start with JSX

2.JSX does not support logical judgment

Class App extend React.Component {render() {return (<div> {if (bool) {<div> <div/>}} </div>);  }}Copy the code

3. Use closure features to simply support if judgment

  • Paste code directly
    // If.js--ES5
    exports.If = function (bool) {
        return function (jsx) {
            return bool ? jsx : null;
        };
    };
    //If.js--ES6
    export default option => bool => bool ? jsx : null;Copy the code
Render () {return (<div> {If (list.length === 0) (<div> </div>)} </div> ); }}Copy the code
  • Notice that the code block is wrapped in (), not {}, because it returns a function that needs to be called again
  • bool ? JSX: NULL This is where the unit expression finally determines what is returned

4. Some people may ask, the above judgment directly write ternary expression JSX is not also support it

  • Yes, that’s all right
  • Ternary expressions in the case of large HTML structure code readability is not good, depending on their own trade-offs and programming habits