Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed prop type The prop is marked as required but its value is `undefined`

I'm using react v4, and I'm trying to list an array in the page "treinamentos", but I'm getting an error as I load the page:

Failed prop type: The prop treinamentos is marked as required in Treinamentos, but its value is undefined.

What am I missing here? is it because of the version I'm using?

enter image description here

Treinamentos:

import React from 'react';
import TreinamentosList from './TreinamentosList';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';

class Treinamentos extends React.Component {
  render() {
    return (
      <div>
        Treinamentos
        <TreinamentosList treinamentos={this.props.treinamentos} />
      </div>
    );
  }
}

Treinamentos.propTypes = {
  treinamentos: PropTypes.array.isRequired
}

function mapStateToProps(state) {
  return {
    treinamentos: state.treinamentos
  }
}

export default connect(mapStateToProps)(Treinamentos);

TreinamentosList:

import React from 'react';
import PropTypes from 'prop-types';

export default function TreinamentosList({ treinamentos }) {
  const emptyMessage = (
    <p>Adicione um treinamento</p>
  );

  const treinamentosList = (
    <p>treinamentos list</p>
  );

  return (
    <div>
      {treinamentos.length === 0 ? emptyMessage : treinamentosList}
    </div>
  );
}

TreinamentosList.propTypes = {
  treinamentos: PropTypes.array.isRequired
}

AppRouter:

import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Greetings from './components/Greetings';
import SignUp from './components/singup/SignUp';
import NoMatch from './components/NoMatch';
import Treinamentos from './components/Treinamentos';
import NavigationBar from './components/NavigationBar';

const AppRouter = () => (
  <Router>
    <div className="container">
      <NavigationBar />
      <Switch>
        <Route path="/" component={Greetings} exact={true}/>
        <Route path="/signup" component={SignUp}/>
        <Route path="/treinamentos" component={Treinamentos}/>
        <Route component={NoMatch}/>
      </Switch>
    </div>
  </Router>
);

export default AppRouter;
like image 772
AND4011002849 Avatar asked Nov 15 '25 09:11

AND4011002849


1 Answers

You definitely are receiving a null object when you request the state from treinamentos reducer, you should check your reducer, return treinamentos as an empty array as a initial state (or whatever your business logic requires).

like image 129
QuarK Avatar answered Nov 18 '25 06:11

QuarK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!