简体   繁体   English

如何保存布局以在每次重新渲染时声明?

[英]How do I save the layout to state each time it re-renders?

I am working on an application that changes the order of a product grid by rearranging tiles. 我正在开发一个通过重新排列图块来更改产品网格顺序的应用程序。 (It is like Packery). (就像Packery)。 I have a renderData function that populates the layout grid with data from a file input. 我有一个renderData函数,用来自文件输入的数据填充布局网格。 That is working fine. 很好 The part that I am struggling with, is how then do I save that "layout" - by that I mean the order of the items - how do I save that to state? 我苦苦挣扎的部分是,然后我该如何保存该“版式”(我的意思是指项目的顺序)如何保存该状态?

When the user re-sorts the times in the UI, I want that layout array to change to reflect the new order. 当用户在UI中对时间进行重新排序时,我希望更改布局数组以反映新的顺序。

It seems so simple, but I just haven't seen yet how to do it. 看起来很简单,但我还没有看到如何做。

import React, { Component } from 'react'
import Papa from 'papaparse'
import GridLayout from 'react-grid-layout' 
import {
    Jumbotron,
    Container,
    Collapse,
    Navbar,
    NavbarToggler,
    NavbarBrand,
    Nav,
    NavItem,
    NavLink,
    UncontrolledDropdown,
    DropdownToggle,
    DropdownMenu,
    DropdownItem,
    Button,
    Input,
} from 'reactstrap'

import '../node_modules/react-grid-layout/css/styles.css'
import '../node_modules/react-resizable/css/styles.css'
import './App.css'

class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            data: [], // on load
            isOpen: false,
        };  

        this.toggle = this.toggle.bind(this);
        this.handleChange = this.handleChange.bind(this);
        this.updateData = this.updateData.bind(this)
        this.renderData = this.renderData.bind(this)       
    }

    toggle() {
        this.setState({
          isOpen: !this.state.isOpen
        });
      }

    handleChange(event) {
        event.preventDefault()
        const inventory = event.target.files[0]
        Papa.parse(inventory, {
            header: true,
            complete: this.updateData
        })
    } // END

    updateData(results) {
        const data = results.data
        this.setState({data}) // {data:data}
    }

    renderData() {
        return  this.state.data.map((item,index) => (  
                    <div className="react-grid-item grid-item" key={item.sku} data-grid={{x: index % 3, y: Math.floor(index / 3), w: 1, h: 1}}>
                        <div> {item.name} </div>
                        <div> {item.gridorder} </div>
                        <div> {item.designer} </div>
                        <img src={item.image} alt="product" />
                        <div> {item.sku} </div>
                        <div> {index} </div>     
                    </div>    
                )) 
    } 

    // QUESTION: How do I save this layout to state each time it rerenders? 

    // NOTE: on click of export button, I want to pass that reordered layout held in state to handleClick and parse it back to CSV
    // handleClick(event) {
    //     Papa.unparse({

    //     })
    // }

    render() {

        return (
            <div>
                <Navbar color="dark" dark expand="md">
                    <NavbarBrand href="/">GO App - grid order tool</NavbarBrand>
                    <NavbarToggler onClick={this.toggle} />
                    <Collapse isOpen={this.state.isOpen} navbar>
                        <Nav className="ml-auto" navbar>
                        <NavItem>
                            <NavLink href="/components/">Components</NavLink>
                        </NavItem>
                        <NavItem>
                            <NavLink href="https://github.com/reactstrap/reactstrap">GitHub</NavLink>
                        </NavItem>
                        <UncontrolledDropdown nav inNavbar>
                            <DropdownToggle nav caret>
                            Options
                            </DropdownToggle>
                            <DropdownMenu right>
                            <DropdownItem>
                                Option 1
                            </DropdownItem>
                            <DropdownItem>
                                Option 2
                            </DropdownItem>
                            <DropdownItem divider />
                            <DropdownItem>
                                Reset
                            </DropdownItem>
                            </DropdownMenu>
                        </UncontrolledDropdown>
                        </Nav>
                    </Collapse>
                </Navbar>

                <Jumbotron >
                    <Container>
                        <h4> Import CSV </h4>
                    <Input type="file" onChange={this.handleChange}  />
                    </Container>
                </Jumbotron>

                <Container className="album ">
                    <div className="note" > BUG: Drag tiles from text, not image </div>
                    <div className="note" > BUG: Drag order will be changed </div>

                    <GridLayout  compactType="horizontal" useCSSTransforms={true} cols={3} margin={[120, 20]} rowHeight={300} className="react-grid-layout grid" width={1200} >
                        {this.renderData()}
                    </GridLayout>

                    <Navbar color="light" light expand="md">
                    <Collapse isOpen={this.state.isOpen} navbar>
                        <Nav className="ml-auto" navbar>
                        <NavItem>
                            <div className="note" > NOTE: The export function is under construction </div>
                            <Button onClick={this.handleClick} color="secondary" size="sm">Export CSV</Button>
                        </NavItem>

                        </Nav>
                    </Collapse>
                </Navbar>

                </Container>
            </div> // END
        );
    }
} // END

export default App



You can move the state one component above and make the grid display of itens a functional component that only receives an array and display the grid. 您可以将状态一个组件移到上方,并使itens的网格显示成为仅接收数组并显示网格的功能组件。

With that said, to sort the list, on the parent component, you call the 话虽如此,要对列表进行排序,请在父组件上调用

this.setState({data: this.state.data.sort(...)})

and pass this to the child that expects an array to display the grid. 并将其传递给期望数组显示网格的子级。 So the render method on the parent component should be: 因此,父组件上的render方法应该是:

render () {
 const { data } = this.state;

 return (
   <ChildComponent itensList={data} />
 );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确设置组件的状态而不会在每次重新渲染时都将其擦除? - How can I properly set my component's state without it being wiped every single time it re-renders? 每当我更改状态变量时,React本机类都会重新渲染 - React native class re-renders every time when I change state variable 如何在 React 中的特定状态值的重新渲染之间添加转换? - How can I add a transition between re-renders of a specific state value in React? 组件在状态更改时重新渲染 - Component re-renders wierdly on state change 如何从使用该钩子的钩子更新组件中的 state 而不会导致无限重新渲染? - How can I update state in a component from a hook where that hook is used without causing infinite re-renders? 如何在重新渲染时优化 ReactJS 中的应用程序 - How to optimize App in ReactJS on re-renders 初始渲染/重新渲染后如何根据DOM节点信息设置状态? (反应) - How to set state based off DOM node info after initial render/re-renders? (React) React-Redux-如何更新状态而不导致不必要的不​​相关组件重新渲染 - React-Redux - How to update a piece of state without causing unnecessary re-renders on unrelated components 如何限制react-redux连接更新重新渲染到特定的状态分支? - How to limit react-redux connect update re-renders to specific state branches? Reactjs:状态改变时重新渲染iframe - Reactjs: re-renders iframes when state changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM