简体   繁体   English

如何有条件地在jsx中添加一个属性

[英]How to conditionally add a property in jsx

I have a simple material UI RadioGroup code which i want to display conditionally in row and column, default mode is column, Below is my code我有一个简单的材料 UI RadioGroup 代码,我想在行和列中有条件地显示它,默认模式是列,下面是我的代码

 <RadioGroup
  aria-label="gender"
  name="gender1"
  value={value}
  onChange={handleChange}
>
  <FormControlLabel value="female" control={<Radio />} label="Female" />
  <FormControlLabel value="male" control={<Radio />} label="Male" />
  <FormControlLabel value="other" control={<Radio />} label="Other" />
</RadioGroup>

Now in order to display it in row, i need to add one poperty row at RadioGroup like below现在为了按行显示它,我需要在RadioGroup添加一个 poperty row ,如下所示

<RadioGroup
  row
  aria-label="gender"
  name="gender1"
  value={value}
  onChange={handleChange}
>

How can i just update one line conditionally?我怎样才能有条件地更新一行? Here is codesandbox link这是代码和框链接

Create an isRow state and use it's value to conditionally render RadioGroup as row or otherwise创建一个isRow状态并使用它的值有条件地将 RadioGroup 呈现为行或其他方式

const [isRow, setIsRow] = useState(true);
<RadioGroup
  row={this.state.isRow}
  aria-label="gender"
  name="gender1"
  value={value}
  onChange={handleChange}
>

我怎样才能指定一个结构<div>有条件地在 JSX 中?</div><div id="text_translate"><p> 我正在尝试在 React return() 语句中创建一个响应式<div> ,但每次我转向时,我的代码在 VSCode 中都会出错。 我为此做出的众多努力之一(以及 chatGPT 推荐的)是:</p><pre> <div> {props.platform === "desktop"? <div className="displaydiv">: <div>} </div> </div></pre><p> 在这种情况下,第一个<div>被 VSCode 错误认为没有结束标记。</p><p> 我不确定这段代码是否会真正运行,即问题是否可能只是过度热情的 VSCode 检查。 但即使这<strong>是</strong>问题所在,这对我也无济于事,因为这些错误会掩盖我项目中的真正错误。</p></div> - How can I specify the structure of a <div> conditionally in JSX?

暂无
暂无

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

相关问题 如何有条件地向对象添加属性 - How can I add a property to an object conditionally 如何在反应 JSX 中有条件地显示元素? - How to conditionally show element in react JSX? 如何在 JSX 中添加超链接 - How to add hyperlink in JSX 如何在 JSX 中添加函数? - How to add function in JSX? ES6:如何仅在该属性存在时有条件地将属性(其本身是一个对象)添加到 object - ES6: How can you add a property (which itself is an object) to an object conditionally only if that property exists 我怎样才能指定一个结构<div>有条件地在 JSX 中?</div><div id="text_translate"><p> 我正在尝试在 React return() 语句中创建一个响应式<div> ,但每次我转向时,我的代码在 VSCode 中都会出错。 我为此做出的众多努力之一(以及 chatGPT 推荐的)是:</p><pre> <div> {props.platform === "desktop"? <div className="displaydiv">: <div>} </div> </div></pre><p> 在这种情况下,第一个<div>被 VSCode 错误认为没有结束标记。</p><p> 我不确定这段代码是否会真正运行,即问题是否可能只是过度热情的 VSCode 检查。 但即使这<strong>是</strong>问题所在,这对我也无济于事,因为这些错误会掩盖我项目中的真正错误。</p></div> - How can I specify the structure of a <div> conditionally in JSX? 如何有条件地渲染两个 JSX 变量? - How can I render two JSX variables conditionally? 如何有条件地将 map 输入到具有一定弹性样式的 JSX 输入字段? - How to map to JSX input fields with a certain flex style conditionally? 如何将带有 JSX 的文件添加到 Html - How to add files with JSX to Html 如何在 props 中添加 JSX? - How to add JSX inside props?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM