我基本上是想在react中创建标签,但有一些问题.

这是page.jsx号文件

<RadioGroup>
    <Button title="A" />
    <Button title="B" />
</RadioGroup>

When you click on button A, the RadioGroup component needs to de-select button B

"Selected"只是指来自一个州或属性的类名

以下是RadioGroup.jsx:

module.exp或ts = React.createClass({

    onChange: function( e ) {
        // How to modify children properties here???
    },

    render: function() {
        return (<div onChange={this.onChange}>
            {this.props.children}
        </div>);
    }

});

Button.jsx的来源并不重要,它有一个常规的HTML单选按钮,可以触发本机DOM onChange事件

The expected flow is:

  • 点击按钮"A"
  • 按钮"A"触发onChange,即本机DOM事件,该事件会冒泡到RadioGroup
  • 调用RadioGroup onChange侦听器
  • 这是我的问题.

这是我遇到的主要问题:I cannot move 100s into 101,因为它的 struct 是这样的,子元素们是arbitrary.也就是说,标记可以是

<RadioGroup>
    <Button title="A" />
    <Button title="B" />
</RadioGroup>

<RadioGroup>
    <OtherThing title="A" />
    <OtherThing title="B" />
</RadioGroup>

I've tried a few things.

RadioGroup的onChange处理程序中有Attempt:个:

React.Children.f或Each( this.props.children, function( child ) {

    // Set the selected state of each child to be if the underlying <input>
    // value matches the child's value

    child.setState({ selected: child.props.value === e.target.value });

});

Problem:

Invalid access to component property "setState" on exp或ts at the top
level. See react-warning-descript或s . Use a static method
instead: <exp或ts />.type.setState(...)

RadioGroup的onChange处理程序中有Attempt:个:

React.Children.f或Each( this.props.children, function( child ) {

    child.props.selected = child.props.value === e.target.value;

});

Problem:什么也没发生,即使我给ButtoncomponentWillReceiveProps方法


Attempt:我试图将父对象的某些特定状态传递给子对象,这样我就可以更新父对象的状态,让子对象自动响应.在RadioGroup的渲染功能中:

React.Children.f或Each( this.props.children, function( item ) {
    this.transferPropsTo( item );
}, this);

Problem:

Failed to make request: Err或: Invariant Violation: exp或ts: You can't call
transferPropsTo() on a component that you don't own, exp或ts. This usually
means you are calling transferPropsTo() on a component passed in as props
或 children.

Bad solution #1: Use react-addons.js cloneWithProps method to clone the children at render time in RadioGroup to be able to pass them properties

Bad solution #2:围绕HTML/JSX实现一个抽象,这样我就可以动态地传递属性(kill me):

<RadioGroup items=[
    { type: Button, title: 'A' },
    { type: Button, title: 'B' }
]; />

然后在RadioGroup中动态构建这些按钮.

This question对我没有帮助,因为我需要在不知道子元素们是什么的情况下把他们交给我

推荐答案

我不知道你为什么说使用cloneWithProps是一个糟糕的解决方案,但这里有一个使用它的工作示例.

var Hello = React.createClass({
    render: function() {
        return <div>Hello {this.props.name}</div>;
    }
});

var App = React.createClass({
    render: function() {
        return (
            <Group ref="buttonGroup">
                <Button key={1} name="Component A"/>
                <Button key={2} name="Component B"/>
                <Button key={3} name="Component C"/>
            </Group>
        );
    }
});

var Group = React.createClass({
    getInitialState: function() {
        return {
            selectedItem: null
        };
    },

    selectItem: function(item) {
        this.setState({
            selectedItem: item
        });
    },

    render: function() {
        var selectedKey = (this.state.selectedItem && this.state.selectedItem.props.key) || null;
        var children = this.props.children.map(function(item, i) {
            var isSelected = item.props.key === selectedKey;
            return React.addons.cloneWithProps(item, {
                isSelected: isSelected,
                selectItem: this.selectItem,
                key: item.props.key
            });
        }, this);

        return (
            <div>
                <strong>Selected:</strong> {this.state.selectedItem ? this.state.selectedItem.props.name : 'None'}
                <hr/>
                {children}
            </div>
        );
    }

});

var Button = React.createClass({
    handleClick: function() {
        this.props.selectItem(this);
    },

    render: function() {
        var selected = this.props.isSelected;
        return (
            <div
                onClick={this.handleClick}
                className={selected ? "selected" : ""}
            >
                {this.props.name} ({this.props.key}) {selected ? "<---" : ""}
            </div>
        );
    }

});


React.renderComponent(<App />, document.body);

这里有一个jsFiddle,展示了它的实际apply.

EDIT:下面是一个更完整的动态选项卡内容示例:jsFiddle

Reactjs相关问答推荐

试图从Amazon-Cogito-Identity-js模拟CogitoUser.authateUser,并将其获取为未定义的S

点击页脚链接时,如何更改ReactJS导航栏中的活动菜单项(样式为Tailwind CSS)?

useEffect不重新渲染

Gitlab CI和VITE环境变量出现问题

有没有比;KeyableShell;组成部分

在迭代状态时无法读取未定义的属性(读取 map )

状态改变不会触发重新渲染

react 本机屏幕转换

在 React 中将 MUI 样式外包到单独的文件中?

在 monorepo 内的工作区或库之间共享 redux 状态

VideoSDK api 不使用更新状态

Formik onChange() 字段挂钩覆盖显示值和 Select 机制

next js 13 在获取中使用标头时动态渲染而不是静态渲染?

页面加载时不显示数组中的数据

list 组件未按预期运行

使用 react-router-dom 时弹出空白页面

Cypress - 替换 React 项目中的变量

我在使用 Elements of stripe 时使用 React router v6

仅在重新渲染后设置状态

警告:您在没有 `onChange` 处理程序的情况下为表单字段提供了 `value` 属性