【问题标题】:Re-Render component React重新渲染组件 React
【发布时间】:2021-06-19 16:04:47
【问题描述】:

我正在开发一个 react 应用程序,当用户切换按钮时,价格会根据用户的偏好发生变化。

在父应用 app.js 中单击按钮后,我无法重新呈现状态。

我的目标:让组件在切换按钮更改时使用更新的值重新渲染。

App.js

import './App.css';
import "./components/Switch.css";
import Pricingbox from './Pricingbox';
import { useState } from "react";

function App() {
  const [price, setPrice] = useState(1);
  const [toggle, setToggle] = useState(false);
  const toggler = () =>{
      toggle ? setToggle(false): setToggle(true);
      console.log(toggle);
      toggle ? setPrice(0): setPrice(1);
    }
  
  return (
    <main>
    <header className="our-pricing">
      <h1 className="h1">Our Pricing</h1>

      <section className="annuall-monthly">
          <div class="space-around">
            <h3 className="h3">Annually</h3>
              <label className="switch1">
                  <input type="checkbox" onClick={() => toggler()}/>
                  <span className="slider1"/>
              </label>
            <h3 className="h3">Monthly</h3>
          </div>
          
      </section>
        <Pricingbox price={price}/>

    </header>
    
    </main>
  );
}
export default App;

Pricingbox.js

import { useEffect, useState } from "react";

const Pricingbox = ({ price }) => {
    console.log(price);
    const pricingList = [
        {
            id: 1, 
            basic: "$19.99",
            prof: "$24.99",
            master: "$39.99"
             
        },
        {
            id: 2, 
            basic: "$199.99",
            prof: "$249.99",
            master: "$399.99"

        }
    ]
        
    const x = pricingList[price];
    console.log(x);
    const y = x.basic; 
    const z = x.prof; 
    const w = x.master;
    const [priceBasic, setPriceBasic] = useState(y);
    const [priceProf, setPriceProf] = useState(z);
    const [priceMaster, setPriceMaster] = useState(w);   

    
        return ( 
            <main>
                
                <div className="price-rectangle white-box">
                    <div className="pricing-text">
                        <p className="pricing-text-space">Basic</p>
                        <h2>{priceBasic}</h2>
                        <p className="bottom-p">
                            <hr></hr>
                            500 GB Storage
                            <hr></hr>
                            2 Users Alllowed
                            <hr></hr>
                            Send up tp 3GB
                            <hr></hr> 
                        </p>
                        <button className="btn purplish-box">Learn More</button>
                    </div>
                </div>
                <div className="price-rectangle purplish-box">
                    <div className="pricing-text">
                        <p className="pricing-text-space">Professional</p>
                        <h2>{priceProf}</h2>
                        <p className="bottom-p">
                            <hr></hr>
                            1 GB Storage
                            <hr></hr>
                            5 Users Alllowed
                            <hr></hr>
                            Send up tp 10GB
                            <hr></hr> 
                        </p>
                        <button className="btn white-box">Learn More</button>
                    </div>
                </div>
                <div className="price-rectangle white-box">
                    <div className="pricing-text">
                        <p className="pricing-text-space">Master</p>
                        <h2>{priceMaster}</h2>
                        <p className="bottom-p">
                            <hr></hr>
                            2 GB Storage
                            <hr></hr>
                            10 Users Alllowed
                            <hr></hr>
                            Send up tp 20GB
                            <hr></hr> 
                        </p>
                        <button className="btn purplish-box">Learn More</button>
                    </div>
                </div>
            </main>
        );
}
 
export default Pricingbox;

【问题讨论】:

    标签: javascript reactjs render


    【解决方案1】:

    在我看来,这可能更简单,建议的 useEffect 是不必要的。我简化了代码,只有一个切换变量而不是两个,并删除了建议的 useEffect 并用一个简单的赋值替换它。显然,如果您需要两个切换开关,则可以将其重新添加。

    import React from 'react';
    import Pricingbox from './Pricingbox';
    import { useState } from "react";
    
    function App() {
      const [toggle, setToggle] = React.useState(false);
      const toggler = () => setToggle(!toggle);
      
      return (
        <main>
        <header className="our-pricing">
          <h1 className="h1">Our Pricing</h1>
          <section className="annuall-monthly">
              <div class="space-around">
                <h3 className="h3">Annually</h3>
                  <label className="switch1">
                      <input type="checkbox" onClick={() => toggler()}/>
                      <span className="slider1"/>
                  </label>
                <h3 className="h3">Monthly</h3>
              </div>
          </section>
            <Pricingbox price={toggle?1:0}/>
        </header>
        </main>
      );
    }
    export default App;
    

    Pricingbox 文件最初包含这一行:

    const [priceBasic, setPriceBasic] = useState(y);
    

    仅当您计划稍后更改定价时才需要这样做。那是你想做的吗?如果您不需要这样做,那么您可以将 Pricingbox 文件简化为:

    import React from 'react';
    import { useEffect, useState } from "react";
    
    const Pricingbox = ({ price }) => {
        const pricingList = [
            {
                id: 1, 
                basic: "$19.99",
                prof: "$24.99",
                master: "$39.99"
                 
            },
            {
                id: 2, 
                basic: "$199.99",
                prof: "$249.99",
                master: "$399.99"
    
            }
        ]
    
        const x = pricingList[price];
        const priceBasic  = x.basic;
        const priceProf   = x.prof;
        const priceMaster = x.master;
       
            return ( 
                <main>
                    
                    <div className="price-rectangle white-box">
                        <div className="pricing-text">
                            <p className="pricing-text-space">Basic</p>
                            <h2>{priceBasic}</h2>
                            <p className="bottom-p">
                                <hr></hr>
                                500 GB Storage
                                <hr></hr>
                                2 Users Alllowed
                                <hr></hr>
                                Send up tp 3GB
                                <hr></hr> 
                            </p>
                            <button className="btn purplish-box">Learn More</button>
                        </div>
                    </div>
                    <div className="price-rectangle purplish-box">
                        <div className="pricing-text">
                            <p className="pricing-text-space">Professional</p>
                            <h2>{priceProf}</h2>
                            <p className="bottom-p">
                                <hr></hr>
                                1 GB Storage
                                <hr></hr>
                                5 Users Alllowed
                                <hr></hr>
                                Send up tp 10GB
                                <hr></hr> 
                            </p>
                            <button className="btn white-box">Learn More</button>
                        </div>
                    </div>
                    <div className="price-rectangle white-box">
                        <div className="pricing-text">
                            <p className="pricing-text-space">Master</p>
                            <h2>{priceMaster}</h2>
                            <p className="bottom-p">
                                <hr></hr>
                                2 GB Storage
                                <hr></hr>
                                10 Users Alllowed
                                <hr></hr>
                                Send up tp 20GB
                                <hr></hr> 
                            </p>
                            <button className="btn purplish-box">Learn More</button>
                        </div>
                    </div>
                </main>
            );
    }
     
    export default Pricingbox;
    

    【讨论】:

      【解决方案2】:

      您可以像这样修复Pricingbox。您可以通过将[price] 设置为useEffect 的第二个参数,在price 更改时更新组件的state

      import { useEffect, useState } from "react";
      
      const Pricingbox = ({ price }) => {
          const pricingList = [
              {
                  id: 1, 
                  basic: "$19.99",
                  prof: "$24.99",
                  master: "$39.99"
                   
              },
              {
                  id: 2, 
                  basic: "$199.99",
                  prof: "$249.99",
                  master: "$399.99"
      
              }
          ];
          const [priceBasic, setPriceBasic] = useState(0);
          const [priceProf, setPriceProf] = useState(0);
          const [priceMaster, setPriceMaster] = useState(0);
          useEffect(() => {
              const x = pricingList[price];
              console.log(x);
              const y = x.basic; 
              const z = x.prof; 
              const w = x.master;
              setPriceBasic(y);
              setPriceProf](z);
              setPriceMaster](w);  
          }, [price]);
      
              return ( 
                  <main>
                      
                      <div className="price-rectangle white-box">
                          <div className="pricing-text">
                              <p className="pricing-text-space">Basic</p>
                              <h2>{priceBasic}</h2>
                              <p className="bottom-p">
                                  <hr></hr>
                                  500 GB Storage
                                  <hr></hr>
                                  2 Users Alllowed
                                  <hr></hr>
                                  Send up tp 3GB
                                  <hr></hr> 
                              </p>
                              <button className="btn purplish-box">Learn More</button>
                          </div>
                      </div>
                      <div className="price-rectangle purplish-box">
                          <div className="pricing-text">
                              <p className="pricing-text-space">Professional</p>
                              <h2>{priceProf}</h2>
                              <p className="bottom-p">
                                  <hr></hr>
                                  1 GB Storage
                                  <hr></hr>
                                  5 Users Alllowed
                                  <hr></hr>
                                  Send up tp 10GB
                                  <hr></hr> 
                              </p>
                              <button className="btn white-box">Learn More</button>
                          </div>
                      </div>
                      <div className="price-rectangle white-box">
                          <div className="pricing-text">
                              <p className="pricing-text-space">Master</p>
                              <h2>{priceMaster}</h2>
                              <p className="bottom-p">
                                  <hr></hr>
                                  2 GB Storage
                                  <hr></hr>
                                  10 Users Alllowed
                                  <hr></hr>
                                  Send up tp 20GB
                                  <hr></hr> 
                              </p>
                              <button className="btn purplish-box">Learn More</button>
                          </div>
                      </div>
                  </main>
              );
      }
       
      export default Pricingbox;
      

      【讨论】:

        猜你喜欢
        • 2018-04-22
        • 1970-01-01
        • 2016-01-11
        • 2020-05-01
        • 2018-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多