0

I am having issues trying to set up a couple of routes in my react web application, and I was hoping someone more knowledgable would be able to help me better understand whats going on.

So, in App.js I am using a creating a BrowserRouter, inside of it I have some HTML, then I also have a few Routes should show a different component on the screen depending on what URL you are at, or if you follow a within those class components:

// imports omitted for simplicity

function App() {
  return (
    <BrowserRouter>
      <NAVBAR />
      <div id="window_container">
        <div id="display_container">
          <Routes>
            <Route path="/" element={() => VIEW_LIST } />
            <Route path="/browse" element={() => BROWSE_ITEMS } />
          </Routes>
        </div>
      </div>
    </BrowserRouter>
  );
}

export default App;

Where I am running into issues is that whenever I try to access one of those URLs in a browser, none of my elements from that component render and I get an error in the console telling me that:

Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.

Where I think my issue lies is somewhere in my component class with how I try to build my component with data retrieved from some endpoint.

Here is an example of what one of my component classes looks like:

export class VIEW_LIST extends Component {
    constructor (props) {
        super(props);
        this.state = {
            list_elements:''
        }
    }

    componentDidMount() {
        getList()
            .then(res => res.json())
            .then(data => {
                console.log(data);
                this.setState({list_elements: data});
                return(
                    <div>
                        I got the data
                    </div>
                )
            })
            .catch(err => {
                console.log(err);
            })
    }

    render () {
        if(this.state.list_elements != null) {
            return (
                <div id="merchant_wrapper">
                </div>
            )
        } else {
            return (
                <div id="merchant_loading"> 
                    Loading...
                </div>
            )
        }
        
    }

}

Does anyone have any idea what might be going on here?

NO THAT DOES NOT ANSWER MY QUESTION STOP DOING THAT TO ALL MY QUESTIONS

backward forward
  • 429
  • 3
  • 17
  • 2
    I don't think there's an `element` prop on `Route`. [Did you mean](https://v5.reactrouter.com/web/api/Route/render-func) ``? I also suggest renaming your components using Pascal case as that's what React expects: ``. – Andy Nov 12 '21 at 01:31
  • @Andy As of V6, component is no longer available and is replaced with element: https://stackoverflow.com/questions/69854011/matched-leaf-route-at-location-does-not-have-an-element – backward forward Nov 12 '21 at 01:38
  • So, based on that documentation perhaps `} />`. – Andy Nov 12 '21 at 01:44
  • 1
    @backwardforward ok but check the syntax for that. You should be doing `element={}`, rather than `element={() => VIEW_LIST }`. `Component` and `` aren't the same – Jayce444 Nov 12 '21 at 01:44
  • Brilliant. I was saying the element was a component class and not the HTML rendered in that class. First to make it an answer wins – backward forward Nov 12 '21 at 01:49

0 Answers0