第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我正在嘗試調(diào)用 switch_func 函數(shù)。但不知何故無法得到想要的結(jié)果

我正在嘗試調(diào)用 switch_func 函數(shù)。但不知何故無法得到想要的結(jié)果

qq_笑_17 2022-07-21 10:32:19
<!DOCTYPE html><html><script src="https://unpkg.com/react@16/umd/react.production.min.js"></script><script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script><script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script><body>  <div id="root"></div><script type="text/babel">class Hello extends React.Component {  constructor(props) {        super(props);        this.state = { myStr: "Initial state" };        this.switch_func = this.switch_func.bind(this);    }        switch_func = () => {  // the setState if commented ...the initial state of myStr is executed ..meaning the function is called however the setState method doesnt work . Can anyone tell why this is happening?          this.setState({ myStr: "yess"});      return (            <div>                <h3>{this.state.myStr}</h3>            </div>        );    }        render() {        return (            <div>                <h1>Hello, world!</h1>                {this.switch_func()}            </div>        );    }}ReactDOM.render(<Hello />, document.getElementById('root'))</script></body></html>即使在綁定了函數(shù) switch_func 之后,它也不會(huì)被執(zhí)行,并且 myStr 的狀態(tài)保持與初始化時(shí)相同。請(qǐng)幫助解決編碼未能顯示所需結(jié)果的地方。誰(shuí)能告訴為什么 setState 方法在這里不起作用?所需的輸出是 myStr 的狀態(tài)更改為 - “是的,它是四個(gè)!”class MyClass extends React.Component {    constructor(props) {        super(props);        this.state = {myStr: "Initial state"};        this.switch_func = this.switch_func.bind(this);    }    switch_func = ()=> {        this.setState({myStr: "In function"});        switch (1 + 3) {            case 2 + 2:                this.setState({ myStr: "Yes its four!"});                break;            default:                this.setState({ myStr: "Oops! default"});        }    return(        <div>            <h3>{this.state.myStr}</h3>        </div>    );  }
查看完整描述

2 回答

?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊

您不應(yīng)該在 render 中調(diào)用 switch 函數(shù),因?yàn)樗赡軙?huì)導(dǎo)致您的組件陷入無限循環(huán) - 每次渲染時(shí),狀態(tài)都會(huì)發(fā)生變化......它會(huì)再次渲染。


此外,在一個(gè)函數(shù)中混合渲染和狀態(tài)更新是非常危險(xiǎn)的 -永遠(yuǎn)不應(yīng)該這樣做,因?yàn)樗赡軙?huì)導(dǎo)致大量?jī)?yōu)化泄漏。


將您的函數(shù)調(diào)用移動(dòng)到另一個(gè)方法,例如componentDidMount():


class MyClass extends React.Component {

    constructor(props) {

        super(props);

        this.state = {myStr: "Initial state"};

    }


    componentDidMount() {

      switchFunc();

    }


    switchFunc = () => {

        switch (1 + 3) {

            case 2 + 2:

                this.setState({ myStr: "Yes its four!"});

                break;

            default:

                this.setState({ myStr: "Oops! default"});

        }

    }


    render(){

        return (

            <div>

                <h1>Hello, world!</h1>

                <div>

                    <h3>{this.state.myStr}</h3>

                </div>

            </div>

        );

    }

}


ReactDOM.render(

  <MyClass />,

  document.getElementById('root')

);

componentDidMountrender是由 React 提供的一種特殊方法 (as ),它允許在安裝組件時(shí)控制組件的行為。更多關(guān)于這些方法的信息:https ://reactjs.org/docs/state-and-lifecycle.html


另外,請(qǐng)注意,暫時(shí)將渲染 myStr 與render方法分開是一種過大的殺傷力 - 就像render我的示例一樣,直接在方法中使用它。


只是一個(gè)旁注 - 嘗試camelCased為您的方法使用名稱,以使它們與其余代碼保持一致(如我更新的示例中所示)。


關(guān)于綁定的另一個(gè)優(yōu)化信息-您添加.bind()了對(duì)構(gòu)造函數(shù)的調(diào)用-不需要,因?yàn)槟鷖witchFunc沒有在類之外的任何上下文中使用(因此,它this始終指向類,因此您不需要.bind()再次訪問上下文) . 此外,更重要的是——你把它寫成一個(gè) lambda ( () => {}) 函數(shù),而不是一個(gè)普通函數(shù)——lambdas 沒有它們的上下文并從它們定義的地方繼承父上下文——因此它總是指向類this上下文而沒有任何顯式綁定


查看完整回答
反對(duì) 回復(fù) 2022-07-21
?
慕少森

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊

您只需通過在末尾添加括號(hào)來調(diào)用該函數(shù)。這將導(dǎo)致無限循環(huán),盡管每次您的組件渲染時(shí),它都會(huì)更新狀態(tài),這反過來又會(huì)導(dǎo)致重新渲染等等。


class MyClass extends React.Component {

    constructor(props) {

        super(props);

        this.state = { myStr: "Initial state" };

        this.switch_func = this.switch_func.bind(this);

    }

    switch_func = () => {

        switch (1 + 3) {

            case 2 + 2:

                this.setState({ myStr: "Yes its four!" });

                break;

            default:

                this.setState({ myStr: "Oops! default" });

        }


        return (

            <div>

                <h3>{this.state.myStr}</h3>

            </div>

        );

    }


    render() {

        return (

            <div>

                <h1>Hello, world!</h1>

                {this.switch_func()}

            </div>

        );

    }

}


ReactDOM.render(

    <MyClass />,

    document.getElementById('root')

);


查看完整回答
反對(duì) 回復(fù) 2022-07-21
  • 2 回答
  • 0 關(guān)注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)