javascript - 如果按下按钮, react 和 JSX : exchange two divs,

标签 javascript css reactjs

我是 React 和 JSX 的新手,我想交换两个 div。我用 Bootstrap 4、JSX 和 React (Typescript) 完成了它。 我有多个带有 cols 的行,并希望它们按 1:1 的比例更改。

`

<row> --- <row>

<col> --- <col>

<col> --- <col>

</row> --- </row>

` ...

How can I do this only with JSX on the css side.
The button use the  this.sidechange on onClick.

                <div className="row">
                    <div className="col">

                    </div>
                    <div className="col">
                        <button type="button" className="btn btn-warning" onClick={this.sideChange}>change the two divs</button>
                    </div>
                    <div className="col">

                    </div>
                </div>


                <div className="row">
                    <div className="col">
                        <h1>test</h1>
                    </div>

                    <div className="col"><h1>test 2</h1>
                    </div>
                </div>

... and more rows and cols.

Code for the button:




sideChange(event:any) {        
        if (stat === 0) {  //
            stat = 1;      //reverse
//code for changing

}
else{
stat = 0;
//code for changing

提前致谢。

最佳答案

class App extends React.Component {
  state = { status: false };

  handleToggle = () => {
    this.setState(prev => ({ status: !prev.status }));
  };
  render() {
    const { status } = this.state;
    return (
      <div>
        <button onClick={this.handleToggle}>Toggle</button>
        <div className="row">
          {status ? (
            <div className="col">
              <h1>test 1</h1>
            </div>
          ) : (
            <div className="col">
              <h1>test 2</h1>
            </div>
          )}
        </div>
      </div>
    );
  }
}

关于javascript - 如果按下按钮, react 和 JSX : exchange two divs,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58852582/

相关文章:

css - 结合 Bootstrap 导航栏和 CSS 网格

php - 使用随机函数创建目录时出错

javascript - 我收到 TypeError : Cannot convert undefined or null to object when using Object. assign

reactjs - 如何在 Next.js 应用程序中使用 ES2020 功能

javascript - 如何求多维数组中的最大值

javascript - Angular4 和 Webpack - 300kb(优化后)用于简单的 "hello world"应用程序?

javascript - 如何使文本框的默认文本变灰?

javascript - 使用 Typescript 在 React JS 中禁用文本框

javascript - javascript 中收到的响应不是 XML,而在浏览器上它显示的是 XML

javascript - 在 Mongoose 中定义变体类型