javascript - 如何在ReactJS中为选定的子组件设置className

标签 javascript reactjs

ReactJs 新手 - 我已经查看了文档 herehere但我有点困惑。

所以我有一个组件可以根据 JSON 数据创建多个表行。

我试图做到这一点,以便一旦选择单选按钮,父级 <td> 的类就会显示出来。设置为“成功”。但目前该列的所有行都具有相同的类名。

var SearchResult = React.createClass({
    getInitialState: function () {
        return {
            site: '',
            address: '',
            data: [],
            checked: ''
        };
    },
    onSiteChanged: function (e) {
        this.setState({
            site: e.currentTarget.value,
            checked: 'success'
        });
    },
    render: function () {
        var resultRows = this.props.data.map(function (result) {
            return (
                <tr>
                    <td className={this.state.checked}>
                        <input type="radio" name="site_name"
                            value={result.SITE_NAME}
                            onChange={this.onSiteChanged}
                            key={result.id}/>{result.SITE_NAME}</td>
                    <td>
                        <input type="radio" name="address"
                            value={result.ADDRESS}
                            onChange={this.onAddressChanged} />{result.ADDRESS}</td>
                </tr>
            );
        }, this);
        return (
            <table className="table table-hover table-condensed">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Address</th>
                    </tr>
                </thead>
                <tbody>
                    {resultRows}
                </tbody>
                <tfoot>
                    <tr>
                        <td></td>
                        <td>{this.state.site}</td>
                        <td>{this.state.address}</td>
                    </tr>
                </tfoot>
            </table>
        );
    }
});

确保所选结果获得所选类名的最佳 ReactJS 方法是什么? 谢谢。

最佳答案

要修改传递给 classSet 属性的值,React 有特殊的插件:React.addons.classSet 。当您更改多个不同的类时,它非常方便,但在您的情况下它也很有用:

var SearchResult = React.createClass({
    getInitialState: function () {
        return {
            site: '',
            address: '',
            checked: false,
            data: [],
        };
    },
    onSiteChanged: function (selected) {
        this.setState({
            site: selected.SITE_NAME,
            checked: selected.id,
        });
    },
    render: function () {
        var resultRows = this.props.data.map(function (result) {
            var cx = React.addons.classSet({
                success: (this.state.checked === result.id)
            });
            return (
                <tr key={result.id}>
                    <td className={cx}>
                        <input type="radio" name="site_name"
                            value={result.SITE_NAME}
                            onChange={this.onSiteChanged.bind(this, result)}
                            />{result.SITE_NAME}</td>
                    <td>
                        <input type="radio" name="address"
                            value={result.ADDRESS}
                            onChange={this.onAddressChanged} />{result.ADDRESS}</td>
                </tr>
            );
        }, this);
        return (
            <table className="table table-hover table-condensed">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Address</th>
                    </tr>
                </thead>
                <tbody>
                    {resultRows}
                </tbody>
                <tfoot>
                    <tr>
                        <td></td>
                        <td>{this.state.site}</td>
                        <td>{this.state.address}</td>
                    </tr>
                </tfoot>
            </table>
        );
    }
});

关于javascript - 如何在ReactJS中为选定的子组件设置className,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27797529/

相关文章:

javascript - 为 node.js 应用程序初始化 redis 数据库的推荐机制是什么?

javascript - 如何从 HTML 标签获取属性值

javascript - 使用 foo 颜色框更改图像

javascript - 使用 React 删除节点并用数组中的项目替换到同一节点位置

javascript - 为什么 React 组件不渲染?

javascript - React 在提交处理程序中找不到 &lt;input/> 值

javascript - 在 Regex/Javascript 中将一个字符换成另一个

reactjs - 具有 Material UI TextField 的 react-hooks-form 相关字段

javascript - 在react.js中每3秒更改一次图像

node.js - 将 ES6 "import"转换为 nodejs "require"的正确方法