0

I have a react component that I use it in some routes. the component needs some data witch is different in routes. The component is like below:

import React from "react";

class ChangeView extends React.Component{
    render(){
        return(
            <div>
                <button type="button" className="btn open-modal" title="change view">
                    <i className="fas fa-eye"></i>
                </button>
                <div id="ms">
                    <ul>{data}</ul>
                </div>
            </div>
        )
    }
}
export default ChangeView;

and a sample route:

import React , {Component} from 'react';
import ChangeView from 'ChangeView';
class Firewall extends Component {
  render() {
    const data = [
       {name:"smith",age:20},
       {name:"nik",age:10},
       {name:"mosh",age:45}
    ];
    return(
      <ChangeView />
    )
  }
}
hamed dehghan
  • 471
  • 7
  • 15
  • Don't use cookies for storing object. Use `localStorage` instead. Here's how you **[set a value](https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem)** and how to **[get a value](https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem)**. – Reinstate Monica Cellio May 25 '19 at 10:57
  • Possible duplicate of [I want to store Javascript array as a Cookie](https://stackoverflow.com/questions/2980143/i-want-to-store-javascript-array-as-a-cookie) and [Create array in cookie with javascript](https://stackoverflow.com/questions/4470477) – adiga May 25 '19 at 11:08

2 Answers2

1

that not valid way to store the cookies, do it like this

document.cookie=  "mycookie=" + json_str;
uingtea
  • 6,002
  • 2
  • 26
  • 40
0

Cookies are not meant to be used for objects or Arraylists and so on. You should only use strings for cookies, you can however make a serialization of that array to a string and add that to the cookie. But this is really not how it is supposed to be used. Also the browser might drop the cookie due to its length.