地图绘制后再次重新渲染

时间:2019-07-26 04:12:05

标签: reactjs redux

我有一个问题,我需要从状态添加数据并使用map对其进行循环。在我的项目中,我使用redux并从redux中获取了其自身的值,但是在映射后,该状态不会再次呈现。

我已经尝试在地图数组中使用this.props.sendNameProduct,但是当我添加新产品时,第一个产品已更新为最后一个产品的值。我只希望它存储第一个产品,然后在我输入第二个产品时,第一个产品仍在那里。

class OpenPositions extends Component {
    constructor(props) {
        super(props);

        this.state = {};
    }

    render() {
        console.log(this.props, "ini porpoaspdopa")
        console.log(this.props.sendNameProduct, "ini send name")
        console.log(this.props.sendNameProduct.sendNameProduct," ini kirik");
        let dapetItem = this.props.cobaSend == null ? [] : this.props.cobaSend
        let cobaSend = [{qty: '123'}];
        let getItem = localStorage.getItem('storage')
        let newData =[];
        let newDataBaru = [];
        let newArr = [];
            newArr.push(this.props.cobaSend);
            newData = newDataBaru.concat(newArr)
        console.log(newArr, "ini new array");
        console.log(newData, "ini new Data")

        return (
            <div style={{height:'100%', overflow:'auto'}}>
                <table className="borderline" cellspacing="0" cellpadding="0" border="0" style={{height:'100%'}}>
                    <tbody>
                        <tr>
                            <td>
                                <div>
                                    <div>       
                                        <table style={{height:"100%", width:"100%"}}>
                                            <tbody>
                                                <tr align="center" className="fieldtitle fieldbg">
                                                    <td align="left">Items</td>
                                                    <td>Qty</td>
                                                    <td>Sold</td>
                                                    <td>Bought</td>
                                                    <td>Avail</td>
                                                    <td>Date</td>
                                                    <td>Options</td>
                                                </tr>
                                            </tbody>

                                            <tbody className="divOP2">

                                                        {this.props.cobaSend.map((item, index) =>{
                                                            console.log(item, "ini item bois");
                                                            console.log(index, "ini index")
                                                            return(
                                                            <tr align="center" className="contentbg">
                                                                <td align="left" nowrap className="bggradientleft" style={{backgroundImage: ' url(https://demo.sgberjangka.com/images/background_gradientleft.gif)', backgroundRepeat: 'repeat-y', backgroundRepeatX: 'repeat', boxSizing: "border-box", border: "1px solid black", backgroundSize:"100%"}}><font color="#FFFFFF"><strong>{item.product}</strong></font></td>
                                                                <td>{item.qty}</td>
                                                                <td>{item.sell}</td>
                                                                <td>{item.buy}</td>
                                                                <td>{item.avail}</td>
                                                                <td>{item.date}</td>
                                                                <td>
                                                                    <input
                                                                        className="checkbox"
                                                                        type="checkbox"
                                                                        checked={this.state.isChecked}
                                                                        onChange={this.handleCheckBox}
                                                                    />
                                                                </td>
                                                            </tr>
                                                        )})}                
                                            </tbody>                        
                                        </table>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <table className="normal" cellspacing="0" cellpadding="0" border="0" style={{height:'100%'}}>
                            <tbody>
                                <tr>
                                    <td>
                                        <button type="submit" className="normalButton wide">GTC Order (Liq)</button>
                                    </td>
                                    <td>
                                        <button type="submit" className="normalButton wide">Buy/Sell (Liq)</button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </tbody>
                </table>
            </div>
        )
}
};

function stateProps(state){
    console.log(state.main.sendNameProduct, "ini send Name Product")
    console.log(state.main.sendValueSell, "ini send sell Product")
    console.log(state.main.sendValueBuy, "ini send buy Product")
    console.log(state.main.sendQuantity, "ini send qty Product")
    return {
        sendNameProduct : state.main,
        sendValueSell : state.main,
        sendValueBuy : state.main,
        sendQuantity : state.main
    };
};

function dispatchProps(dispatch){
    return {

    }
}


export default connect(
    stateProps,
    dispatchProps
)(OpenPositions);

当我用新数据更新时,第一个结果更改并与新数据具有相同的值。 让我们假设this.props.cobaSend在映射中得到了{产品:“ HKK50_BBJ”,数量:“ 12”,出售:“ 28579”,购买:“-”,有效:“ 12”,…}值乘积,数量,销售等,但是当我插入新数据时,映射中的值不会随新数组更新。

3 个答案:

答案 0 :(得分:0)

您的newData.mapnewData array上循环,但在您的DOM中显示this.props.sendNameProduct...值。

在访问这些值时可能只是一个错误,请尝试通过地图回调调用item返回:

{
    newData.map((item, index) =>{
        console.log(item, "ini item bois");
        console.log(index, "ini index")
        return(
            <tr align="center" className="contentbg">
            <td align="left" nowrap className="bggradientleft"><font color="#FFFFFF"><strong>{item.item}</strong></font></td>
            <td>{item.qty}</td>
            <td>{item.sell}</td>
            <td>{item.buy}</td>
            <td>{item.date}</td>
            <td>{item.avail}</td>
            <td>
                <input
                    className="checkbox"
                    type="checkbox"
                    checked={this.state.isChecked}
                    onChange={this.handleCheckBox}
                />
            </td>
        </tr>
        )
    });
} 

答案 1 :(得分:0)

让数据= this.state.data;

    let newData = [];
    let newDataBaru = [];
        data.push(data[0])
     newData = newDataBaru.concat(data)

通过上述操作,您总是将第一个元素(即data [0])推入数组中,我认为这是错误的。它必须始终一次又一次地推送相同的数据,因为如果将任何内容推送到数组中,它将转到最后一个索引

答案 2 :(得分:0)

class OpenPositions extends Component {
    constructor(props) {
        super(props);

        this.state = {
            newArr : []
        };
    }

    render() {
        console.log(this.props, "ini porpoaspdopa")
        console.log(this.props.sendNameProduct, "ini send name")
        console.log(this.props.sendNameProduct.sendNameProduct," ini kirik");
        console.log(this.props.cobaSend," ini COBA SEND");
        // let dapetItem = this.props.cobaSend == null ? [] : this.props.cobaSend
        // let cobaSend = [{qty: '123'}];
        // let getItem = localStorage.getItem('storage')
        let newArr = this.state.newArr;
            newArr.push(this.props.cobaSend);
        console.log(newArr, "ini new array");
        console.log(newData, "ini new Data")
        //let newData = [];
        //let newDataBaru = newData.concat(data);
            //newData.push(data[0])
            //console.log(newData, "ini new Data")
            //console.log(newDataBaru,"ini data baru")
         //newData = newDataBaru.concat(data)
        //  for(let i =0 ; i< 5; i++){



        //  }
        //console.log(newData, "ini new Data")

        return (
            <div style={{height:'100%', overflow:'auto'}}>
                <table className="borderline" cellspacing="0" cellpadding="0" border="0" style={{height:'100%'}}>
                    <tbody>
                        <tr>
                            <td>
                                <div>
                                    <div>       
                                        <table style={{height:"100%", width:"100%"}}>
                                            <tbody>
                                                <tr align="center" className="fieldtitle fieldbg">
                                                    <td align="left">Items</td>
                                                    <td>Qty</td>
                                                    <td>Sold</td>
                                                    <td>Bought</td>
                                                    <td>Avail</td>
                                                    <td>Date</td>
                                                    <td>Options</td>
                                                </tr>
                                            </tbody>

                                            <tbody className="divOP2">

                                                        {newArr.map((item, index) =>{
                                                            console.log(item, "ini item bois");
                                                            console.log(index, "ini index")
                                                            return(
                                                            <tr align="center" className="contentbg">
                                                                <td align="left" nowrap className="bggradientleft" style={{backgroundImage: ' url(https://demo.sgberjangka.com/images/background_gradientleft.gif)', backgroundRepeat: 'repeat-y', backgroundRepeatX: 'repeat', boxSizing: "border-box", border: "1px solid black", backgroundSize:"100%"}}><font color="#FFFFFF"><strong>{item.product}</strong></font></td>
                                                                <td>{item.qty}</td>
                                                                <td>{item.sell}</td>
                                                                <td>{item.buy}</td>
                                                                <td>{item.avail}</td>
                                                                <td>{item.date}</td>
                                                                <td>
                                                                    <input
                                                                        className="checkbox"
                                                                        type="checkbox"
                                                                        checked={this.state.isChecked}
                                                                        onChange={this.handleCheckBox}
                                                                    />
                                                                </td>
                                                            </tr>
                                                        )})}                
                                            </tbody>                        
                                        </table>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <table className="normal" cellspacing="0" cellpadding="0" border="0" style={{height:'100%'}}>
                            <tbody>
                                <tr>
                                    <td>
                                        <button type="submit" className="normalButton wide">GTC Order (Liq)</button>
                                    </td>
                                    <td>
                                        <button type="submit" className="normalButton wide">Buy/Sell (Liq)</button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </tbody>
                </table>
            </div>
        )
    }
};

im使用状态将其存储在那里,然后在im输入数据时起作用。