0

I am currently working on my webapp and in one of its components I am getting some DB data with :

var SelectHund = React.createClass({
          getInitialState: function() {
            return {
              hunde:{}
            };
          },
          componentDidMount: function(){
            var that = this;
            $.ajax({
              type: "POST",
              url: "/foodoo/index.php?method=getDogs",
              data: JSON.stringify(planData),
              success: function(response){
                var hunde = JSON.parse(response);
                console.log(hunde);
                if(hunde.length >= 1){

                  that.setState({
                    hunde: hunde
                  });
                }
              },
              error: function(){
              }
            });
          },
          render: function(){
            if(this.state.hunde.length <= 0){
              return(<div>test</div>);
            }else{
              // console.log(this.state);
              // console.log(this.state.hunde);
              var POS = this.state.hunde.map(function(i){
                  console.log(i);
                  return  <option key={i.id} value={i.weight}>i.name</option>;
              });
              return(<select className="selectHund" name="hund">{POS}</select>);
            }
          }
        });

The component gets called like this:

return (
                    <div>
                      <h2>{this.state.planname}<span className="close">x</span></h2>
                      <SelectHund changeHund={this.changeHund} />
...

With that method I run through the response object with the map function, the console.log of "i" returns the single objects like following :

Object {id: "1", user_id: "1", name: "Balou", weight: "15", activity: "1"…}
Object {id: "2", user_id: "1", name: "Franzi", weight: "70", activity: "1"…}

So I guess there is no error with the object handling.

Unfortunately I keep getting this error in the console :

Uncaught Error: Invariant Violation: SelectHund.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.

I have tried calling an external function outside of the ajax function to see if this may cause the error but it doesn't. Some other amateur tries to debug it failed as well - So I need your help ^^

4
  • with SelectHund.render(), I'd expect to see a tag such as: <SelectHund onChange={that.props.changeHund} className="selectHund" name="hund"> {POS} </SelectHund> Commented Aug 12, 2015 at 19:44
  • I don't know exactly what you mean - but I have updated my question with the whole component syntax as well as the component call in the parent component Commented Aug 12, 2015 at 19:47
  • So first glance, don't couple your data fetch to a component. If you are using flux, do a fetch inside a grid store. Commented Aug 12, 2015 at 19:57
  • I added it to the component did mount function now but cant get it to work either - cant find a way to update the state with the response value properly and then map throuhg it to create multiple options - updated question Commented Aug 12, 2015 at 20:28

1 Answer 1

2

Your render method does not return anything. You need to move this line:

return(<select onChange={that.props.changeHund} className="selectHund" name="hund">{POS}</select>);

to outside the success callback. Then you will be facing other problems, but it’s in the right direction.

Instead of doing Ajax inside the render function, move it to another lifecycle method (like componentDidMount) or a data model like Flux.

Then use states or props to handle internal component data (in your example, the hunde object). Following some examples from the docs/tutorials should get you started.

Sign up to request clarification or add additional context in comments.

3 Comments

Can't get it working - I nowed added the ajax call to the componentdidmount function but I cant seem a proper way to set the state with the response value so that I can use it for a dynamic output like my previous method -> thats how my component looks now : pastebin.com/ENMXLK1n
You’ll get there, it looks like you are on the right track :)
Haha thanks for that motivation speech :p I am in a rush because I won't have time to deal with it untill 2pm and its due today :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.