React MUI – 在动态创建的TextField元素上操作

huangapple go评论83阅读模式
英文:

React MUI - Operating on dynamically created TextField elements

问题

我尝试动态添加按钮和文本字段对,并使按钮对其相应的文本字段执行某些操作。我的特定用例是创建一个“用户设置”页面,用户可以更改特定帐户信息属性。

以下是一个生成2对文本字段和按钮的最小代码示例。但是,按下任何一个按钮会禁用两个文本字段,而不仅仅是它对应的文本字段:

在上面的示例中,如何使按钮按下后只禁用其关联的文本字段?

我当前的天真方法用于读取动态创建元素的值,是给文本字段分配id属性字段,并操作DOM以读取值(例如,在onClick={(e) => handleChange(e)}方法中使用document.getElementById(e.target.id).value),但这违反了React的原则。

英文:

I am trying to dynamically add pairs of buttons and text fields and have the button perform some action on its corresponding text field. My specific use case is creating a 'User Settings' page where users can change a specific account information property

Here is a minimal code example that generates 2 pairs of text fields and buttons. However, pressing either button disables both text fields rather than its corresponding text field:

export default function FAQ() {

    const [disabled, toggleDisabled] = React.useState(false)

    const CreateInputPair = () => {
        return (
            <>
                <TextField
                    disabled={disabled}
                />
                <Button
                    onClick={() => toggleDisabled(!disabled)}
                >
                    Toggle Disabled
                </Button>
            </>
        )
    }

    return (
        <>
            <CreateInputPair />
            <br />
            <CreateInputPair />
        </>
    );

}

In the above example, how can I make it so that pressing a button only disables its associated text field?

My current and naive approach for reading values of dynamically created elements is to assign id prop fields to the TextField and manipulate the DOM to read the value (ie. having document.getElementById(e.target.id).value inside a onClick={(e) => handleChange(e)} method), but this goes against the principles of React

答案1

得分: 1

CreateInputPair移动到自己单独的组件中。

const InputPair = () => {
	const [disabled, toggleDisabled] = React.useState(false);
	return (
		<>
			<TextField
				disabled={disabled}
			/>
			<Button
				onClick={() => toggleDisabled(!disabled)}
			>
				Toggle Disabled
			</Button>
		</>
	)
}
export default function FAQ() {
	return <>
		<InputPair /> <br /> <InputPair />
	</>;
}
英文:

Move CreateInputPair into its own separate component.

const InputPair = () =&gt; {
	const [disabled, toggleDisabled] = React.useState(false);
	return (
		&lt;&gt;
			&lt;TextField
				disabled={disabled}
			/&gt;
			&lt;Button
				onClick={() =&gt; toggleDisabled(!disabled)}
			&gt;
				Toggle Disabled
			&lt;/Button&gt;
		&lt;/&gt;
	)
}
export default function FAQ() {
	return &lt;&gt;
		&lt;InputPair /&gt; &lt;br /&gt; &lt;InputPair /&gt;
	&lt;/&gt;;
}

答案2

得分: 1

import { useState } from "react";

export default function FAQ() {
const [disabled, toggleDisabled] = useState();

const toggleDisabledInput = (id) => {
if (disabled === id) {
toggleDisabled(null);
} else {
toggleDisabled(id);
}
}

console.log(disabled);

const CreateInputPair = ({ id }) => {
return (
<>
<input disabled={disabled === id} />
<button onClick={() => toggleDisabledInput(id)}>Toggle Disabled
</>
);
};

return (
<>
// you can pass id from your db


</>
);
}

英文:

I hope this helps

import { useState } from &quot;react&quot;;

export default function FAQ() {
  const [disabled, toggleDisabled] = useState();

  const toggleDisabledInput = (id) =&gt; {
    if (disabled === id) {
      toggleDisabled(null);
    } else {
      toggleDisabled(id);
    }
  };

  console.log(disabled);

  const CreateInputPair = ({ id }) =&gt; {
    return (
      &lt;&gt;
        &lt;input disabled={disabled === id} /&gt;
        &lt;button onClick={() =&gt; toggleDisabledInput(id)}&gt;Toggle Disabled&lt;/button&gt;
      &lt;/&gt;
    );
  };

  return (
    &lt;&gt;
      &lt;CreateInputPair id=&quot;1&quot; /&gt; // you can pass id from your db
      &lt;br /&gt;
      &lt;CreateInputPair id=&quot;2&quot; /&gt;
    &lt;/&gt;
 );
}

huangapple
  • 本文由 发表于 2023年3月20日 23:18:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定