简体   繁体   English

ReactJS:text-align证明不起作用

[英]ReactJS: text-align justify not working

I have been using text-align: justify to evenly distribute menus. 我一直在使用text-align: justify来均匀分配菜单。 Following this tutorial and it is working perfectly. 按照本教程 ,它完美地运行。

However, it breaks when I use ReactJS to create the view. 但是,当我使用ReactJS创建视图时,它会中断。 A comparison can be found here: http://jsfiddle.net/j7pLprza/1/ . 可以在这里找到比较: http//jsfiddle.net/j7pLprza/1/ I use these two simple components to populate the menus: 我使用这两个简单的组件来填充菜单:

var MenuItem = React.createClass({
    render: function() {
        return (
            <li>
                {this.props.title}
            </li>
        );
    }
});

var TopMenus = React.createClass({
    render: function() {
        return (
            <div className="break">
            <nav>
                <ul>
                    {this.props.menus.map(function(menu) {
                        return (<MenuItem title={menu.title} />);
                    })}
                </ul>
            </nav>
            </div>
        );
    }
});

After exploration, I think it is caused by ReactJS, which removes all line-break and white-space after <li> items. 经过探索,我认为它是由ReactJS引起的,它会在<li>项之后删除所有换行符和空格。 This will disable text-align: justify . 这将禁用text-align: justify

It happens for AngularJS as well (but I can fix it by using a add-space-all directive). 它也适用于AngularJS(但我可以通过使用add-space-all指令来修复它)。

I reached only this issue after googling: https://github.com/facebook/jsx/issues/19 . 我在google搜索后只得到了这个问题: https//github.com/facebook/jsx/issues/19 But I quickly got lost. 但我很快迷路了。

I tried to add some extra content, such as {'\\n'} and {' '} , but ReactJS reports syntax errors. 我尝试添加一些额外的内容,例如{'\\n'}{' '} ,但ReactJS会报告语法错误。

Please help. 请帮忙。 Thanks. 谢谢。


UPDATE 1: 更新1:

Following the accepted answer, my menus works in Chrome Emulator. 根据接受的答案,我的菜单适用于Chrome模拟器。 However, when I visit from iPhone 5c (Chrome browser), the result is as if the extra space is not recognized. 但是,当我从iPhone 5c(Chrome浏览器)访问时,结果就好像没有识别出额外的空间。

After trying different combinations, this one works: 尝试不同的组合后,这个组合起作用:

var TopMenus = React.createClass({
    render: function() {
        return (
            <div className="break">
            <nav>
                <ul>
                    {this.props.menus.map(function(menu) {
                        return [(<MenuItem title={menu.title} />), '&nbsp; '];
                    })}
                </ul>
            </nav>
            </div>
        );
    }
});

Please be noted that the extra space in &nbsp; 请注意,额外的空间在&nbsp; is necessary. 有必要的。 Missing either &nbsp; 缺少&nbsp; or 要么 breaks it. 打破它。

It works then on Nexus 7, iPhone 5c (Chrome and Safari). 它适用于Nexus 7,iPhone 5c(Chrome和Safari)。 However, I do not know the exact reason. 但是,我不知道具体原因。 If anyone knows, please help. 如果有人知道,请帮助。


UPDATE 2: 更新2:

Still buggy. 还有马车。 So I switched to Flex layout . 所以我切换到Flex布局 And it is super easy. 这非常容易。 An example is: http://jsfiddle.net/j7pLprza/4/ 一个例子是: http//jsfiddle.net/j7pLprza/4/

React doesn't remove anything in this case, since there is no whitespace between elements in the first place. 在这种情况下,React不会删除任何内容,因为首先在元素之间没有空格。 If you want to add whitespaces between the li elements, you can interleave the array with ' ' . 如果要在li元素之间添加空格,可以将数组与' '交错。 In this case it is as simple as returning an array from the .map function (arrays are flattened): 在这种情况下,它就像从.map函数返回数组一样简单(数组被展平):

var TopMenus = React.createClass({
    render: function() {
        return (
            <div className="break">
            <nav>
                <ul>
                    {this.props.menus.map(function(menu) {
                        return [<MenuItem title={menu.title} />, ' '];
                    })}
                </ul>
            </nav>
            </div>
        );
    }
});

Example: https://jsfiddle.net/j7pLprza/2 示例: https//jsfiddle.net/j7pLprza/2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM