0

Constructor code which contains the data

constructor(props){
    super(props);
    this.state = {
        Data: [
            {
                "name": 'red'
            },
            {
                "name": 'green'
            },
            {
                "name": 'red'
            },
            {   
               "name": 'brown'
            },
            {
                "name": 'yellow'
            },
            {
                "name": 'brown'
            }
        ]
    }
}

The code for my button where I map the data

{this.state.Data.map((color) => (
                        <>
                        <div className="ViewDetailsBtn"><Button onClick={this.clickMe.bind(this, color.name)} className="ViewDetailsBtnLink">View Details</Button></div></>
                        
                    ))}

onClick function Code

clickMe = (details) => {
    console.log(details);
    this.props.history.push({
        pathname: "/ViewDetails", 
        state: {detail: details}
    });
}

Here it displays the color name on my console properly and it redirects me to ViewDetails but how do I display the color name on the ViewDetails page?

ViewDetails page code

    import React from 'react'
    const App = (props) => {
    console.log(props);
    //const data = props.location.state.detail;
    return (
        <div>
            <h1>Details:-</h1>
            {/* <h1>{data}</h1> */}
        </div>
    )
   }

   export default App
AB7zz
  • 82
  • 1
  • 1
  • 6
  • 1
    you can do props.location.state.data – Shyam May 28 '21 at 07:57
  • So should I type in "const data = props.location.state.data"? and should I display it by "

    {data}

    "? I'm pretty new to this.
    – AB7zz May 28 '21 at 08:04
  • it depends on what you have in `data` . if your data is a string then you can do what you said above . – Shyam May 28 '21 at 08:07
  • Yea, it's a string. But it's not working :/. Getting an error "TypeError: Cannot read property 'data' of undefined". I've updated the code in the question for ViewDetails – AB7zz May 28 '21 at 08:10
  • Is your `ViewDetails` component rendered via `Route` ? . Also add a `console.log(props)` and check whether you are able to see `location` in it . – Shyam May 28 '21 at 08:13
  • The way you are adding state in `props.history.push` is wrong . Please refer this - https://stackoverflow.com/questions/44121069/how-to-pass-params-with-history-push-link-redirect-in-react-router-v4 – Shyam May 28 '21 at 08:15
  • How is `ViewDetails` rendered? What routing/navigation package are you using? More context is necessary. Can you provide us a [Minimal, ***Complete***, and ***Reproducible*** Code Example](https://stackoverflow.com/help/minimal-reproducible-example)? – Drew Reese May 28 '21 at 08:15
  • It's rendered through Route. I added a console.log(props) in my ViewDetails page. It displays the location in the console too. It says "hash: "" key: "6ekowq" pathname: "/ViewDetails" search: """ But the color name can't be seen. I've edited the question again with more context on what I'm trying to achieve. – AB7zz May 28 '21 at 08:28
  • The main problem is, I can't add a state to the props. Pathname is working, but state isn't.. – AB7zz May 28 '21 at 08:50
  • From your limited code snippet I see no reason why route state *wouldn't* be accessible. Can you create a *running* codesandbox that reproduces this issue that we can live inspect and debug in? – Drew Reese May 28 '21 at 10:01
  • https://codesandbox.io/s/muddy-forest-0n5ky?file=/src/index.js I'm not sure why It's not displaying anything, but here is the full code. Would really appreciate it if you could help me in displaying the color onClick in ViewDetails page. – AB7zz May 28 '21 at 10:44

1 Answers1

1

your ViewDetails component should accept a parameter: const ViewDetails = (props) => {} then you should be able to access that data through that parameter

rcbxd
  • 92
  • 3
  • Could you also tell how to acces the data through the parameter? How should I define the parameter? – AB7zz May 28 '21 at 08:01