账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    react 条件渲染
    22
    0

    刚接触react
    为什么条件渲染不成功,不管warrantyChecked为true还是false,都显示。刚接触react

    function Warranty(props) {
      const [warrantyChecked, setChecked] = useState(true);
    
      useEffect(() => {
        alert(`count发生变动,最新值为${warrantyChecked}`);
      }, [warrantyChecked])
    
      return (
        <div>
          <List className="warranty-list">
            <List.Item extra={<Switch name="switch" checked={warrantyChecked} onChange={()=> setChecked(!warrantyChecked)}/>} >服务<span className="list-des">(免费维修)</span></List.Item>
            { {warrantyChecked} &&
              <>
                <List.Item arrow="right" extra="共1件">商品</List.Item>
                <List.Item extra="1年">期限</List.Item>
                <List.Item extra="¥16.00">合计</List.Item>
              </>
            }
          </List>
        </div>
      );
    }
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 浅唱_↘幸諨 普通会员 1楼

      在React中,条件渲染是一种基于props的渲染模式。它允许你在组件渲染时,只渲染当props值为某个特定值时的子组件。

      例如,假设你有一个父组件,它有三个子组件,每个子组件都有一个名为"message"的props。你可能有一个条件,只有当"message"为"hello"时,才渲染这两个子组件。

      在React中,你可以这样做:

      ```jsx import React, { useState } from 'react';

      function Parent() { const [message, setMessage] = useState('hello');

      return (

      ); }

      function Child(props) { return (

      {props.message}

      ); }

      export default Parent; ```

      在这个例子中,当"message"的值为"hello"时,这两个子组件都会被渲染。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部