您现在的位置是:主页 > news > 网站在线答题怎么做/咖啡seo是什么意思

网站在线答题怎么做/咖啡seo是什么意思

admin2025/5/1 23:50:08news

简介网站在线答题怎么做,咖啡seo是什么意思,做任务有q币的网站,购买网站做友情链接原文地址:https://facebook.github.io/react/docs/conditional-rendering.html 在React中,您可以创建不同的组件来封装所需的行为。然后,您可以只渲染其中的一些,具体取决于应用程序的state。 eg: function UserGreeting(props) {…

网站在线答题怎么做,咖啡seo是什么意思,做任务有q币的网站,购买网站做友情链接原文地址:https://facebook.github.io/react/docs/conditional-rendering.html 在React中,您可以创建不同的组件来封装所需的行为。然后,您可以只渲染其中的一些,具体取决于应用程序的state。 eg: function UserGreeting(props) {…

原文地址:https://facebook.github.io/react/docs/conditional-rendering.html

在React中,您可以创建不同的组件来封装所需的行为。然后,您可以只渲染其中的一些,具体取决于应用程序的state。

eg:

    function UserGreeting(props) {return <h1>Welcome back!</h1>;}function GuestGreeting(props) {return <h1>Please sign up.</h1>;}function Greeting(props){const isLoggedIn = props.isLoggedIn;if(isLoggedIn){return <UserGreeting />;}return <GuestGreeting />;}ReactDOM.render(<Greeting isLoggedIn={true}/>,document.getElementById('example'));

一、元素变量(Element Variables)

可以使用变量来存储元素。可以有条件地渲染组件的一部分,而输出的其余部分不会更改。

class LoginControl extends React.Component {constructor(props) {super(props);this.handleLoginClick = this.handleLoginClick.bind(this);this.handleLogoutClick = this.handleLogoutClick.bind(this);this.state = {isLoggedIn: false};}handleLoginClick() {this.setState({isLoggedIn: true});}handleLogoutClick() {this.setState({isLoggedIn: false});}render() {const isLoggedIn = this.state.isLoggedIn;let button = null;if (isLoggedIn) {button = <LogoutButton onClick={this.handleLogoutClick} />;} else {button = <LoginButton onClick={this.handleLoginClick} />;}return (<div><Greeting isLoggedIn={isLoggedIn} />{button}</div>);}
}function UserGreeting(props) {return <h1>Welcome back!</h1>;
}function GuestGreeting(props) {return <h1>Please sign up.</h1>;
}function Greeting(props) {const isLoggedIn = props.isLoggedIn;if (isLoggedIn) {return <UserGreeting />;}return <GuestGreeting />;
}function LoginButton(props) {return (<button onClick={props.onClick}>Login</button>);
}function LogoutButton(props) {return (<button onClick={props.onClick}>Logout</button>);
}ReactDOM.render(<LoginControl />,document.getElementById('root')
);

二、逻辑&&运算符的内联If(Inline If with Logical && Operator)

可以在JSX中嵌入任何表达式,方法是将其包裹在花括号中。这包括JavaScript逻辑&&运算符。

function Mailbox(props) {const unreadMessages = props.unreadMessages;return (<div><h1>Hello!</h1>{unreadMessages.length > 0 &&<h2>You have {unreadMessages.length} unread messages.</h2>}</div>);
}const messages = ['React', 'Re: React', 'Re:Re: React'];
ReactDOM.render(<Mailbox unreadMessages={messages} />,document.getElementById('root')
);

它的作用是因为在JavaScript中,(true &&表达式)结果是为表达式,并且(false &&表达式)结果为false。

三、条件运算符的内联if-Else(Inline If-Else with Conditional Operator)

condition ? true : false.

render() {const isLoggedIn = this.state.isLoggedIn;return (<div>{isLoggedIn ? (<LogoutButton onClick={this.handleLogoutClick} />) : (<LoginButton onClick={this.handleLoginClick} />)}</div>);
}

四、防止组件渲染(Preventing Component from Rendering)

在极少数情况下,您可能希望组件隐藏自身,即使它由另一个组件渲染。 为此,返回null替代其渲染输出。

function WarningBanner(props) {if (!props.warn) {return null;}return (<div className="warning">Warning!</div>);
}class Page extends React.Component {constructor(props) {super(props);this.state = {showWarning: true}this.handleToggleClick = this.handleToggleClick.bind(this);}handleToggleClick() {this.setState(prevState => ({showWarning: !prevState.showWarning}));}render() {return (<div><WarningBanner warn={this.state.showWarning} /><button onClick={this.handleToggleClick}>{this.state.showWarning ? 'Hide' : 'Show'}</button></div>);}
}ReactDOM.render(<Page />,document.getElementById('root')
);

从组件的render方法返回null不会影响组件生命周期方法的触发。 例如,componentWillUpdate和componentDidUpdate仍将被调用。

转载于:https://www.cnblogs.com/xmyun/p/6963105.html