React Component array not updating












0














I'm learning react, and I'm stuck with not updating list components.
The component shows all of the list elements that I add manually, but not rendering any changes.



I searched a lot for solutions.
All of my change handlers are binded, the setState inside handleSubmit should update ClockRow...



My App.js:



import React, { Component } from 'react';
import Clock from './Clock';
import ClockRow from './ClockRow';

class App extends Component {
constructor(props) {
super(props);
this.state = {items: , tle: 'Teszt', ival: 200};

this.handleChangeTitle = this.handleChangeTitle.bind(this);
this.handleChangeInterval = this.handleChangeInterval.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChangeTitle(event) {
this.setState({tle: event.target.value});
}

handleChangeInterval(event) {
this.setState({ival: event.target.value});
}

handleSubmit(event) {
if(this.state.tle.length > 0 && this.state.ival > 9){
this.setState({items: [...this.state.items, <Clock interval={this.state.ival} title={this.state.tle} />]});
}

event.preventDefault();
}

render(){
return (
<div>
<div className="row">
<h1 className="col text-center">Hello, React!</h1>
</div>
<div className="row">
<form onSubmit={this.handleSubmit}>
<label>
Title: <input type="text" name="tle" value={this.state.tle} onChange={this.handleChangeTitle} />
</label>
<label>
Interval: <input type="number" name="ival" value={this.state.ival} onChange={this.handleChangeInterval} />
</label>
<input type="submit" value="Add" />
</form>
</div>
<ClockRow clockItems={this.state.items} />
</div>
);
}
}

export default App;


My ClockRow.js:



import React, { Component } from 'react';

class ClockRow extends Component{
constructor(props){
super(props);
this.state = {clocks: props.clockItems.map((x, i) => <div className="col" key={i}>{x}</div>) }
}

render(){
return(<div className="row">{this.state.clocks}</div>
)};
}

export default ClockRow;


My Clock.js:



import React, { Component } from 'react';
import {Card, CardTitle, CardBody, CardFooter} from 'reactstrap';

class Clock extends Component {
constructor(props){
super(props);
this.state = {counter: 0, interval: parseInt(props.interval), title: props.title};
}

componentDidMount() {
this.timerID = setInterval(() => this.tick(), this.state.interval);
}

componentWillUnmount() {
clearInterval(this.timerID);
}

tick() {
this.setState((state) => ({
counter: state.counter + 1
}));
}

render() {
return (
<Card>
<CardTitle>{this.state.title}</CardTitle>
<CardBody>{this.state.counter}</CardBody>
<CardFooter>{this.state.interval}</CardFooter>
</Card>
);
}
}

export default Clock;









share|improve this question






















  • Put regular data in your component state and derive the JSX from that data in the render method. You generally don't want to store React.createElement results in your state. If you take props and add it to your state, the state will not update when the props update. It's therefore a good idea to just use the props directly if you can, and the component will keep itself updated automatically.
    – Tholle
    Nov 11 at 20:19


















0














I'm learning react, and I'm stuck with not updating list components.
The component shows all of the list elements that I add manually, but not rendering any changes.



I searched a lot for solutions.
All of my change handlers are binded, the setState inside handleSubmit should update ClockRow...



My App.js:



import React, { Component } from 'react';
import Clock from './Clock';
import ClockRow from './ClockRow';

class App extends Component {
constructor(props) {
super(props);
this.state = {items: , tle: 'Teszt', ival: 200};

this.handleChangeTitle = this.handleChangeTitle.bind(this);
this.handleChangeInterval = this.handleChangeInterval.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChangeTitle(event) {
this.setState({tle: event.target.value});
}

handleChangeInterval(event) {
this.setState({ival: event.target.value});
}

handleSubmit(event) {
if(this.state.tle.length > 0 && this.state.ival > 9){
this.setState({items: [...this.state.items, <Clock interval={this.state.ival} title={this.state.tle} />]});
}

event.preventDefault();
}

render(){
return (
<div>
<div className="row">
<h1 className="col text-center">Hello, React!</h1>
</div>
<div className="row">
<form onSubmit={this.handleSubmit}>
<label>
Title: <input type="text" name="tle" value={this.state.tle} onChange={this.handleChangeTitle} />
</label>
<label>
Interval: <input type="number" name="ival" value={this.state.ival} onChange={this.handleChangeInterval} />
</label>
<input type="submit" value="Add" />
</form>
</div>
<ClockRow clockItems={this.state.items} />
</div>
);
}
}

export default App;


My ClockRow.js:



import React, { Component } from 'react';

class ClockRow extends Component{
constructor(props){
super(props);
this.state = {clocks: props.clockItems.map((x, i) => <div className="col" key={i}>{x}</div>) }
}

render(){
return(<div className="row">{this.state.clocks}</div>
)};
}

export default ClockRow;


My Clock.js:



import React, { Component } from 'react';
import {Card, CardTitle, CardBody, CardFooter} from 'reactstrap';

class Clock extends Component {
constructor(props){
super(props);
this.state = {counter: 0, interval: parseInt(props.interval), title: props.title};
}

componentDidMount() {
this.timerID = setInterval(() => this.tick(), this.state.interval);
}

componentWillUnmount() {
clearInterval(this.timerID);
}

tick() {
this.setState((state) => ({
counter: state.counter + 1
}));
}

render() {
return (
<Card>
<CardTitle>{this.state.title}</CardTitle>
<CardBody>{this.state.counter}</CardBody>
<CardFooter>{this.state.interval}</CardFooter>
</Card>
);
}
}

export default Clock;









share|improve this question






















  • Put regular data in your component state and derive the JSX from that data in the render method. You generally don't want to store React.createElement results in your state. If you take props and add it to your state, the state will not update when the props update. It's therefore a good idea to just use the props directly if you can, and the component will keep itself updated automatically.
    – Tholle
    Nov 11 at 20:19
















0












0








0







I'm learning react, and I'm stuck with not updating list components.
The component shows all of the list elements that I add manually, but not rendering any changes.



I searched a lot for solutions.
All of my change handlers are binded, the setState inside handleSubmit should update ClockRow...



My App.js:



import React, { Component } from 'react';
import Clock from './Clock';
import ClockRow from './ClockRow';

class App extends Component {
constructor(props) {
super(props);
this.state = {items: , tle: 'Teszt', ival: 200};

this.handleChangeTitle = this.handleChangeTitle.bind(this);
this.handleChangeInterval = this.handleChangeInterval.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChangeTitle(event) {
this.setState({tle: event.target.value});
}

handleChangeInterval(event) {
this.setState({ival: event.target.value});
}

handleSubmit(event) {
if(this.state.tle.length > 0 && this.state.ival > 9){
this.setState({items: [...this.state.items, <Clock interval={this.state.ival} title={this.state.tle} />]});
}

event.preventDefault();
}

render(){
return (
<div>
<div className="row">
<h1 className="col text-center">Hello, React!</h1>
</div>
<div className="row">
<form onSubmit={this.handleSubmit}>
<label>
Title: <input type="text" name="tle" value={this.state.tle} onChange={this.handleChangeTitle} />
</label>
<label>
Interval: <input type="number" name="ival" value={this.state.ival} onChange={this.handleChangeInterval} />
</label>
<input type="submit" value="Add" />
</form>
</div>
<ClockRow clockItems={this.state.items} />
</div>
);
}
}

export default App;


My ClockRow.js:



import React, { Component } from 'react';

class ClockRow extends Component{
constructor(props){
super(props);
this.state = {clocks: props.clockItems.map((x, i) => <div className="col" key={i}>{x}</div>) }
}

render(){
return(<div className="row">{this.state.clocks}</div>
)};
}

export default ClockRow;


My Clock.js:



import React, { Component } from 'react';
import {Card, CardTitle, CardBody, CardFooter} from 'reactstrap';

class Clock extends Component {
constructor(props){
super(props);
this.state = {counter: 0, interval: parseInt(props.interval), title: props.title};
}

componentDidMount() {
this.timerID = setInterval(() => this.tick(), this.state.interval);
}

componentWillUnmount() {
clearInterval(this.timerID);
}

tick() {
this.setState((state) => ({
counter: state.counter + 1
}));
}

render() {
return (
<Card>
<CardTitle>{this.state.title}</CardTitle>
<CardBody>{this.state.counter}</CardBody>
<CardFooter>{this.state.interval}</CardFooter>
</Card>
);
}
}

export default Clock;









share|improve this question













I'm learning react, and I'm stuck with not updating list components.
The component shows all of the list elements that I add manually, but not rendering any changes.



I searched a lot for solutions.
All of my change handlers are binded, the setState inside handleSubmit should update ClockRow...



My App.js:



import React, { Component } from 'react';
import Clock from './Clock';
import ClockRow from './ClockRow';

class App extends Component {
constructor(props) {
super(props);
this.state = {items: , tle: 'Teszt', ival: 200};

this.handleChangeTitle = this.handleChangeTitle.bind(this);
this.handleChangeInterval = this.handleChangeInterval.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChangeTitle(event) {
this.setState({tle: event.target.value});
}

handleChangeInterval(event) {
this.setState({ival: event.target.value});
}

handleSubmit(event) {
if(this.state.tle.length > 0 && this.state.ival > 9){
this.setState({items: [...this.state.items, <Clock interval={this.state.ival} title={this.state.tle} />]});
}

event.preventDefault();
}

render(){
return (
<div>
<div className="row">
<h1 className="col text-center">Hello, React!</h1>
</div>
<div className="row">
<form onSubmit={this.handleSubmit}>
<label>
Title: <input type="text" name="tle" value={this.state.tle} onChange={this.handleChangeTitle} />
</label>
<label>
Interval: <input type="number" name="ival" value={this.state.ival} onChange={this.handleChangeInterval} />
</label>
<input type="submit" value="Add" />
</form>
</div>
<ClockRow clockItems={this.state.items} />
</div>
);
}
}

export default App;


My ClockRow.js:



import React, { Component } from 'react';

class ClockRow extends Component{
constructor(props){
super(props);
this.state = {clocks: props.clockItems.map((x, i) => <div className="col" key={i}>{x}</div>) }
}

render(){
return(<div className="row">{this.state.clocks}</div>
)};
}

export default ClockRow;


My Clock.js:



import React, { Component } from 'react';
import {Card, CardTitle, CardBody, CardFooter} from 'reactstrap';

class Clock extends Component {
constructor(props){
super(props);
this.state = {counter: 0, interval: parseInt(props.interval), title: props.title};
}

componentDidMount() {
this.timerID = setInterval(() => this.tick(), this.state.interval);
}

componentWillUnmount() {
clearInterval(this.timerID);
}

tick() {
this.setState((state) => ({
counter: state.counter + 1
}));
}

render() {
return (
<Card>
<CardTitle>{this.state.title}</CardTitle>
<CardBody>{this.state.counter}</CardBody>
<CardFooter>{this.state.interval}</CardFooter>
</Card>
);
}
}

export default Clock;






reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 20:15









Sarkanyolo

53




53












  • Put regular data in your component state and derive the JSX from that data in the render method. You generally don't want to store React.createElement results in your state. If you take props and add it to your state, the state will not update when the props update. It's therefore a good idea to just use the props directly if you can, and the component will keep itself updated automatically.
    – Tholle
    Nov 11 at 20:19




















  • Put regular data in your component state and derive the JSX from that data in the render method. You generally don't want to store React.createElement results in your state. If you take props and add it to your state, the state will not update when the props update. It's therefore a good idea to just use the props directly if you can, and the component will keep itself updated automatically.
    – Tholle
    Nov 11 at 20:19


















Put regular data in your component state and derive the JSX from that data in the render method. You generally don't want to store React.createElement results in your state. If you take props and add it to your state, the state will not update when the props update. It's therefore a good idea to just use the props directly if you can, and the component will keep itself updated automatically.
– Tholle
Nov 11 at 20:19






Put regular data in your component state and derive the JSX from that data in the render method. You generally don't want to store React.createElement results in your state. If you take props and add it to your state, the state will not update when the props update. It's therefore a good idea to just use the props directly if you can, and the component will keep itself updated automatically.
– Tholle
Nov 11 at 20:19














1 Answer
1






active

oldest

votes


















0














CodeSandbox:
https://codesandbox.io/s/zxlzzv05n3




  • ClockRow.js is surplus

  • Clock.js is not changed


  • App.js is changed, and "React styled":



    import React, { Component } from "react";
    import Clock from "./Clock";

    class App extends Component {
    constructor(props) {
    super(props);
    this.state = { items: , inputTle: "Teszt", inputIval: 200 };
    }

    handleChangeTitle = event => {
    this.setState({ inputTle: event.target.value });
    };

    handleChangeInterval = event => {
    this.setState({ inputIval: event.target.value });
    };

    handleSubmit = event => {
    console.log(this.state);

    if (this.state.inputTle.length > 0 && this.state.inputIval > 9) {
    this.setState(prevState => {
    return {
    items: [
    ...prevState.items,
    {
    title: this.state.inputTle,
    interval: this.state.inputIval
    }
    ]
    };
    });
    }

    event.preventDefault();
    };

    render() {
    return (
    <div>
    <div className="row">
    <h1 className="col text-center">Hello, React!</h1>
    </div>
    <div className="row">
    <form onSubmit={this.handleSubmit}>
    <label>
    Title:{" "}
    <input
    type="text"
    name="tle"
    value={this.state.inputTle}
    onChange={this.handleChangeTitle}
    />
    </label>
    <label>
    Interval:{" "}
    <input
    type="number"
    name="ival"
    value={this.state.inputIval}
    onChange={this.handleChangeInterval}
    />
    </label>
    <input type="submit" value="Add" />
    </form>
    </div>

    <div className="row">
    {this.state.items.map((item, index) => (
    <div className="col" key={index}>
    <Clock {...item} />
    </div>
    ))}
    </div>
    </div>
    );
    }
    }

    export default App;







share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252804%2freact-component-array-not-updating%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    CodeSandbox:
    https://codesandbox.io/s/zxlzzv05n3




    • ClockRow.js is surplus

    • Clock.js is not changed


    • App.js is changed, and "React styled":



      import React, { Component } from "react";
      import Clock from "./Clock";

      class App extends Component {
      constructor(props) {
      super(props);
      this.state = { items: , inputTle: "Teszt", inputIval: 200 };
      }

      handleChangeTitle = event => {
      this.setState({ inputTle: event.target.value });
      };

      handleChangeInterval = event => {
      this.setState({ inputIval: event.target.value });
      };

      handleSubmit = event => {
      console.log(this.state);

      if (this.state.inputTle.length > 0 && this.state.inputIval > 9) {
      this.setState(prevState => {
      return {
      items: [
      ...prevState.items,
      {
      title: this.state.inputTle,
      interval: this.state.inputIval
      }
      ]
      };
      });
      }

      event.preventDefault();
      };

      render() {
      return (
      <div>
      <div className="row">
      <h1 className="col text-center">Hello, React!</h1>
      </div>
      <div className="row">
      <form onSubmit={this.handleSubmit}>
      <label>
      Title:{" "}
      <input
      type="text"
      name="tle"
      value={this.state.inputTle}
      onChange={this.handleChangeTitle}
      />
      </label>
      <label>
      Interval:{" "}
      <input
      type="number"
      name="ival"
      value={this.state.inputIval}
      onChange={this.handleChangeInterval}
      />
      </label>
      <input type="submit" value="Add" />
      </form>
      </div>

      <div className="row">
      {this.state.items.map((item, index) => (
      <div className="col" key={index}>
      <Clock {...item} />
      </div>
      ))}
      </div>
      </div>
      );
      }
      }

      export default App;







    share|improve this answer


























      0














      CodeSandbox:
      https://codesandbox.io/s/zxlzzv05n3




      • ClockRow.js is surplus

      • Clock.js is not changed


      • App.js is changed, and "React styled":



        import React, { Component } from "react";
        import Clock from "./Clock";

        class App extends Component {
        constructor(props) {
        super(props);
        this.state = { items: , inputTle: "Teszt", inputIval: 200 };
        }

        handleChangeTitle = event => {
        this.setState({ inputTle: event.target.value });
        };

        handleChangeInterval = event => {
        this.setState({ inputIval: event.target.value });
        };

        handleSubmit = event => {
        console.log(this.state);

        if (this.state.inputTle.length > 0 && this.state.inputIval > 9) {
        this.setState(prevState => {
        return {
        items: [
        ...prevState.items,
        {
        title: this.state.inputTle,
        interval: this.state.inputIval
        }
        ]
        };
        });
        }

        event.preventDefault();
        };

        render() {
        return (
        <div>
        <div className="row">
        <h1 className="col text-center">Hello, React!</h1>
        </div>
        <div className="row">
        <form onSubmit={this.handleSubmit}>
        <label>
        Title:{" "}
        <input
        type="text"
        name="tle"
        value={this.state.inputTle}
        onChange={this.handleChangeTitle}
        />
        </label>
        <label>
        Interval:{" "}
        <input
        type="number"
        name="ival"
        value={this.state.inputIval}
        onChange={this.handleChangeInterval}
        />
        </label>
        <input type="submit" value="Add" />
        </form>
        </div>

        <div className="row">
        {this.state.items.map((item, index) => (
        <div className="col" key={index}>
        <Clock {...item} />
        </div>
        ))}
        </div>
        </div>
        );
        }
        }

        export default App;







      share|improve this answer
























        0












        0








        0






        CodeSandbox:
        https://codesandbox.io/s/zxlzzv05n3




        • ClockRow.js is surplus

        • Clock.js is not changed


        • App.js is changed, and "React styled":



          import React, { Component } from "react";
          import Clock from "./Clock";

          class App extends Component {
          constructor(props) {
          super(props);
          this.state = { items: , inputTle: "Teszt", inputIval: 200 };
          }

          handleChangeTitle = event => {
          this.setState({ inputTle: event.target.value });
          };

          handleChangeInterval = event => {
          this.setState({ inputIval: event.target.value });
          };

          handleSubmit = event => {
          console.log(this.state);

          if (this.state.inputTle.length > 0 && this.state.inputIval > 9) {
          this.setState(prevState => {
          return {
          items: [
          ...prevState.items,
          {
          title: this.state.inputTle,
          interval: this.state.inputIval
          }
          ]
          };
          });
          }

          event.preventDefault();
          };

          render() {
          return (
          <div>
          <div className="row">
          <h1 className="col text-center">Hello, React!</h1>
          </div>
          <div className="row">
          <form onSubmit={this.handleSubmit}>
          <label>
          Title:{" "}
          <input
          type="text"
          name="tle"
          value={this.state.inputTle}
          onChange={this.handleChangeTitle}
          />
          </label>
          <label>
          Interval:{" "}
          <input
          type="number"
          name="ival"
          value={this.state.inputIval}
          onChange={this.handleChangeInterval}
          />
          </label>
          <input type="submit" value="Add" />
          </form>
          </div>

          <div className="row">
          {this.state.items.map((item, index) => (
          <div className="col" key={index}>
          <Clock {...item} />
          </div>
          ))}
          </div>
          </div>
          );
          }
          }

          export default App;







        share|improve this answer












        CodeSandbox:
        https://codesandbox.io/s/zxlzzv05n3




        • ClockRow.js is surplus

        • Clock.js is not changed


        • App.js is changed, and "React styled":



          import React, { Component } from "react";
          import Clock from "./Clock";

          class App extends Component {
          constructor(props) {
          super(props);
          this.state = { items: , inputTle: "Teszt", inputIval: 200 };
          }

          handleChangeTitle = event => {
          this.setState({ inputTle: event.target.value });
          };

          handleChangeInterval = event => {
          this.setState({ inputIval: event.target.value });
          };

          handleSubmit = event => {
          console.log(this.state);

          if (this.state.inputTle.length > 0 && this.state.inputIval > 9) {
          this.setState(prevState => {
          return {
          items: [
          ...prevState.items,
          {
          title: this.state.inputTle,
          interval: this.state.inputIval
          }
          ]
          };
          });
          }

          event.preventDefault();
          };

          render() {
          return (
          <div>
          <div className="row">
          <h1 className="col text-center">Hello, React!</h1>
          </div>
          <div className="row">
          <form onSubmit={this.handleSubmit}>
          <label>
          Title:{" "}
          <input
          type="text"
          name="tle"
          value={this.state.inputTle}
          onChange={this.handleChangeTitle}
          />
          </label>
          <label>
          Interval:{" "}
          <input
          type="number"
          name="ival"
          value={this.state.inputIval}
          onChange={this.handleChangeInterval}
          />
          </label>
          <input type="submit" value="Add" />
          </form>
          </div>

          <div className="row">
          {this.state.items.map((item, index) => (
          <div className="col" key={index}>
          <Clock {...item} />
          </div>
          ))}
          </div>
          </div>
          );
          }
          }

          export default App;








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 22:02









        Boris Traljić

        464310




        464310






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252804%2freact-component-array-not-updating%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation