【问题标题】:ReactJS react-grid-layout toggle static propertyReactJS react-grid-layout 切换静态属性
【发布时间】:2018-05-11 18:19:38
【问题描述】:

我学习 React 已经有几天了,这次我试图创建一个可拖动的小部件网格,我在 github 上找到了这个库:react-grid-layout 可以做到这一点。

这一次,我尝试在单击 button 时切换可拖动或 static 属性,但这样做时遇到了一些麻烦。

这是我的App.js

import React, { Component } from 'react';
import {InputText} from 'primereact/components/inputtext/InputText';
import GridLayout from 'react-grid-layout';
import './App.css';

class App extends Component {
    render() {
        var layout = [
            {i: 'a', x: 0, y: 0, w: 10, h: 4},
            {i: 'b', x: 1, y: 0, w: 3, h: 2},
            {i: 'c', x: 4, y: 0, w: 1, h: 2}
        ];
        return (
            <React.Fragment>
                <button onClick={this.toggleStatic (layout)}>Enable</button>
                <GridLayout className="layout" layout={layout} cols={30} rowHeight={30} width={1200} onDragStop={this.onDragStop}>
                    <div key="a">a</div>
                    <div key="b">b</div>
                    <div key="c">c</div>
                </GridLayout>
            </React.Fragment>
        );
    }

    toggleStatic(layout) {
        console.log('Layout', layout);
    }

    onDragStop(layout) {
        layout[0].static = true;
        console.log(layout);
    }
}

export default App;

我的index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import 'react-grid-layout/css/styles.css';
import 'react-resizable/css/styles.css';
import 'primereact/resources/themes/omega/theme.css';
import 'primereact/resources/primereact.min.css';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

还有我的index.css

body {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
}

.react-grid-item:not(.react-grid-placeholder) {
    background-color: #ccc;
    border: 1px solid black;
}

.layout {
    background-color: #333;
}

.title {
    font-weight: bold;
}

如果我检查我的控制台,在重新加载页面后,我会在任何点击之前登录我的layout

Layout […]
​0: Object { i: "a", x: 0, y: 0, … }
1: Object { i: "b", x: 1, y: 0, … }    ​
2: Object { i: "c", x: 4, y: 0, … }    ​
length: 3
__proto__: Array []

但如果我将button 更改为:

<button onClick={this.toggleStatic}>Enable</button>

即从中删除参数layout,我在控制台中得到这个输出:

Layout Proxy
    <target>: Object { … }
    <handler>: Object { … }

现在,当拖动任何组件时,我的代码将第一个小部件的 static 更改为 true,我正在尝试通过单击按钮来更改它。

我在图书馆的问题上找到了这篇文章:Assign static dinamically/programatically,但他们使用的是react-redux

我发现他们也在使用这个:

const mapStateToProps = state => {
  return {
    ...state.dashboard.asMutable()
  };
};

但我不确定这是做什么的。

任何建议至少将layout var 接收到toggleStatic 对我来说可能就足够了。

【问题讨论】:

    标签: javascript reactjs draggable react-grid-layout


    【解决方案1】:

    {this.toggleStatic (layout)} 会触发render 而不是onClick 上的功能,所以你必须改变:

    <button onClick={this.toggleStatic (layout)}>Enable</button>
    

    到:

    <button onClick={() => this.toggleStatic(layout)}>Enable</button>
    

    然后,当您单击按钮时,您将获得布局。

    state中添加布局:

    // ...
    class App extends Component {
        state = {
             layout: [
                {i: 'a', x: 0, y: 0, w: 10, h: 4},
                {i: 'b', x: 1, y: 0, w: 3, h: 2},
                {i: 'c', x: 4, y: 0, w: 1, h: 2}
             ]
        }
        render() {
            return (
                <React.Fragment>
                    <button onClick={() => this.toggleStatic(this.state.layout)}>Enable</button>
                    <GridLayout className="layout" layout={this.state.layout} cols={30} rowHeight={30} width={1200} onDragStop={this.onDragStop}>
                        <div key="a">a</div>
                        <div key="b">b</div>
                        <div key="c">c</div>
                    </GridLayout>
                </React.Fragment>
            );
        }
    
    // ...
    

    然后在toggleStatic 函数中执行如下操作:

    toggleStatic(layout){
        var newLayout = layout.map(l => {
               return {...l, static: !l.static || true}
        })
        this.setState({
            layout: newLayout
        })
    }
    

    【讨论】:

    • 你说得对,这实际上将layout 发送到我的方法,但我发现它与我要更新的layout 不同,因为我的对象根本没有受到影响通过那里所做的更改...
    • 我收到Line 18: 'layout' is not defined no-undef; Line 18&lt;button... 的位置
    • 我的错,应该是this.state.layout,我编辑了anwser。
    • 我可以知道...l 是做什么的吗?这是我第二次看到它并想了解更多。更具体地说是 3 个点
    • 这里表示对象中的所有参数,你只想改变对象lstatic参数,所以你保持其余不变...l并设置static新值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    相关资源
    最近更新 更多