-1

This is a question more about the general way that reactjs handles rerendering and how components in arrays work.

So I have a function that creates an array of components but the users props is empty if I do it as follows:

// General look of the component
// <StartMultiple key={this.count} users={this.state.users} id={this.count} delete_this={this.delete_this}/>

// Just an example
this.array = []
add_repeat() // do this 5 times, for example

render(
    {this.array} // users prop is empty
)

add_repeat()
{   
    this.repeats.push(<StartMultiple key={this.count} users={this.state.users} id={this.count} delete_this={this.delete_this}/>);
    this.setState({ repeats: this.repeats }); 
    this.count++;
} 

But if I just stick a <StartMultiple key={this.count} users={this.state.users} id={this.count} delete_this={this.delete_this}/> directly into the return()render() it updates its props as appropriate. (being updated by a this.setState())

So my question is, is there a way to fix this empty array props inside the component array, or should I just look to display them in another way? E.g. map()

A. L
  • 11,695
  • 23
  • 85
  • 163

1 Answers1

1

React components don't belong in state, react state should contain serializable application state, not components which are visual representation of that state.

Please see here, under the "what shouldn't go in state":

http://web.archive.org/web/20150419023006/http://facebook.github.io/react/docs/interactivity-and-dynamic-uis.html#what-shouldnt-go-in-state

Chris
  • 54,599
  • 30
  • 149
  • 186
  • It was really just a test. The way it's going to function on live is that it takes in some data from a backend, then uses that data to create the array of components. But I think it was also due to the fact that I was using promises so that it created the array BEFORE the data was updated. However, I was expecting the components to re-render when the state did. – A. L Jan 06 '17 at 05:35
  • Cool. Even without the async stuff, your original snippet shouldn't work, try to concentrate on storing data in your state, not the visual representation – Chris Jan 06 '17 at 05:37
  • Well I managed to get it working (there are results in user before it creates them). Since they're just static information, I think it will be okay. But just to clarify, sine I'm putting this data into a component OUTSIDE of the render, that means they won't recognise being re-rendered, correct? – A. L Jan 06 '17 at 05:38
  • The behaviour of re-rendering (among other behaviour) is now completely unknown and can't be relied on in a production app because you are doing something that the react docs say don't do. – Chris Jan 06 '17 at 05:40
  • Hmm, I'll think about it I guess. – A. L Jan 06 '17 at 05:42
  • A question for you if you don't mind. I've answered a few of your questions over the last few days (and so have other users) and you seem very intent on not doing things the react way. React makes things easier by making certain assumptions, especially around data flow, but by fighting it, you are making things quite difficult for yourself. Have you used other frameworks before? Or has it been more vanilla js? Either way is fine, just curious as to your motivations around doing it differently – Chris Jan 06 '17 at 05:44
  • This is pretty much the first framework I've used. I tried out Angular 2 just before this, but that was complicated as hell. So I'm quite used to the vanilla way. I'm not so much against the react way, I'm just not used to/sure of the react way lol – A. L Jan 06 '17 at 06:04
  • Cool, I wish you luck. Having used both angular and react, react is much easier to reason about. I do encourage to try to stick to the react-way of doing things though - 99% of functionality can be done this way. Having built big and small react apps, there are few times you can't achieve a feature doing things the react way. – Chris Jan 06 '17 at 06:08
  • the other main problem i have with react is npm packages. Like i try to use bootstrap-select and it just isn't working as intended. As well as the importing of the packages – A. L Jan 06 '17 at 06:55