logo
down
shadow

Changing order of React components only triggers re-render after second click


Changing order of React components only triggers re-render after second click

Content Index :

Changing order of React components only triggers re-render after second click
Tag : javascript , By : robinator
Date : November 28 2020, 04:01 AM

I hope this helps you . The problem is that you're a) sorting in the setState callback and b) your sort is mutating dives instead of setting state.dives to a new, sorted array via setState. This is because render isn't called if state mutates directly - only if setState is called. Instead you can perform the sort in the select function and set sortBy and dives in a single setState call.
select(event) {
  const sortBy = event.target.innerText;
  const sortByParam = sortBy.toLowerCase();
  this.setState({
    sortBy,
    dives: [...this.state.dives.sort(Utilities.sortByParam(sortByParam))];
  });
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Can I guarantee the render order of an array of React components?


Tag : arrays , By : Liy
Date : March 29 2020, 07:55 AM
it fixes the issue This is not expected behavior. React always renders arrays in the order of the elements.
If there are items with conflicting key values, only the latest element will render, but that doesn't look like the case here, since all four of your elements are rendered.

How to render components on click plus without changing the 'state'?


Tag : javascript , By : jaime
Date : March 29 2020, 07:55 AM
may help you . It appears that your component is not being wrapped by any component. In essence, you're telling React that it should render regardless of what route gets matched by the router. So whether your routes matches /first, /second or any other route, React will still render because it "matches" all routes.

Why doesn't react component render when trying to render it in another components click function


Tag : javascript , By : Ben Humphrys
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Don't return UI elements in click handler as it won't render. Just set a flag inside handler and use it to display the Day component inside your render function.
class DayRow extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
    days: Array(7).fill(null),
    showDay: false
  };
  this.handler = this.handler.bind(this);
  this.renderDay = this.renderDay.bind(this);
}

handler() {
  this.setState({
    showDay: true,
  });
}

renderDayComponent(day) {
  let daysWeather = myData[day];
  console.log("now we've reached this far"); //this console log is reached when a button is clicked.
  return (
    <Day
      humidity={daysWeather.humidity}
      temperature={daysWeather.temperature}
    />
  );
}

renderDay(day) {
  return (
    <div>
      <button
        className="day"
        onClick={() => {
          this.handler();
        }}
      >
        {day}
      </button>
      {this.state.showDay && this.renderDayComponent(day)}
    </div>
  );
}

render() {
  return (
    <div>
      <div className="day-row">
        {this.renderDay("Monday")}
        {this.renderDay("Tuesday")}
        {this.renderDay("Wednesday")}
        {this.renderDay("Thursday")}
        {this.renderDay("Friday")}
        {this.renderDay("Saturday")}
        {this.renderDay("Sunday")}
      </div>
    </div>
  );
}
}

Tag : javascript , By : user135518
Date : March 29 2020, 07:55 AM
wish of those help You could keep a value in state, e.g. called panelKey, that you use as key prop for the main panel. You then give panelKey a new value when the the link is clicked, and React will throw away the main panel and create an entirely new component.
Example
class Timer extends React.Component {
  timer = null;
  state = { count: 0 };

  componentDidMount() {
    this.timer = setInterval(() => {
      this.setState(prevState => ({ count: prevState.count + 1 }));
    }, 1000);
  }

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

  render() {
    return <div>{this.state.count}</div>;
  }
}

class MainPanel extends React.Component {
  state = { panelKey: 1 };

  onClick = () => {
    this.setState(prevState => ({ panelKey: prevState.panelKey + 1 }));
  };

  render() {
    return (
      <div key={this.state.panelKey}>
        <button onClick={this.onClick}> Reset Panel </button>
        <Timer />
      </div>
    );
  }
}

ReactDOM.render(<MainPanel />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="root"></div>

React changing order of components does not trigger a re-render


Tag : javascript , By : Alan Little
Date : March 29 2020, 07:55 AM
To fix the issue you can do The problem seems to be that the order changes, but since the keys are based off the i index variable, the recorded list still has the same keys in the same order. React therefore does not see a difference.
Example:
<div key="1"> //app b
<div key="2"> //app a
<div key="3"> //app c
<div key="1"> //app a
<div key="2"> //app b
<div key="3"> //app c
export default class Herokus extends React.Component {
  render() {
    var s = this;

    return (
      <div>
        {s.state.apps.map((app, i) => {
          return (
            <Heroku app={app.app} compact key={`heroku-${app.app}`} updateParrent={s.updateParrent.bind(s)}/>
          );
        })}
      </div>
    );
  }
}
Related Posts Related QUESTIONS :
  • How to attach an event handler only once continuously
  • How to make button open a link in new tab in JavaScript?
  • Find range from the javascript object
  • How can I use my jQuery code as JavaScript?
  • How to show 5 images in a 2 row slider with bxslider
  • How to convert stream into variables in typescript
  • Three.js custom shader error on Chrome GL_INVALID_OPERATION: Active draw buffers with missing fragment shader outputs
  • My JavaScript doesn't work. How can I solve this problem?
  • Why is this async function is running synchronously?
  • Get updated variable from within react hook before return
  • I want to show all image preview but whenever I upload a new one the old one is gone
  • Would I be able to give access to specific electron APIs safely?
  • Why several clicks fire for each instance of a Class?
  • cant grab value from div using attr()
  • Make web service not accessible by browser?
  • Select DOM elements within template
  • How to customize angular ui-grid row background color?
  • Binding data in two respective $scope in AngularJS, how?
  • react put some props after import class
  • Variable is distributed among same components
  • switch class in jQuery/JavaScript for active/inactive state
  • How To Track Outbound Clicks Using Javascript
  • How to get total seconds elapsed since the beginning of the month?
  • Protractor: Stale element reference at "browser.wait(EC.visibilityOf(confDial), FIFTY_SECONDS);"
  • Access to an array inside an object
  • Reseting an JavaScript Interval with different value?
  • Learning Angular for the first time - version 2 or 4?
  • Rendering RSS feed (XML) in React with jquery, why do object values parsed from the feed seem empty?
  • Node.js xml2js http.request tag matching
  • applying texture to custom plane geometry in Threejs
  • How do I pass an Event as a function parameter in HTML written inside JavaScript variable?
  • Interface of object parameter with default value and default property
  • Does using try / catch rather than `.catch` observable operator with XHR requests reduce performance?
  • In IE11 background colors not working on printing
  • TypeScript interface signature "(): string"
  • Is synchronous XMLHttpRequest supported in Internet Explorer 10 and 11?
  • Axios function not returning any values with js
  • How to use firestore TIMESTAMP to create time-stamp in JavaScript
  • Instantiate subclass without constructing
  • Outputing a created object in javascript to a server side JSON file
  • Transition the numeric labels in a bar chart
  • createBottomTabNavigator: hide just one tab from the tabBar
  • iife vs simple statement behavior with document.title
  • showDialogPopup Refresh Page after Click
  • Child component does not set the initial value passed from the parent: ReactJS
  • Nodejs javascript added to button not responding
  • Trying to define a promise.all
  • Python GEE to extract featurecollection timeseries from Landsat imageries
  • How to write negative binary number?
  • Unable to set property of individual object
  • Why Named Function Expression itself cannot assign Name to another Value?
  • How to reset scroll position on single page pagination
  • How to scroll to bottom of page, when a new message is sent - Socket.io
  • How to reconstruct audio blob from a base64 encoded String?
  • How to calculate numbers within an array of objects
  • Using JavaScript to change CSS properties of SVG circles for all circles that was not clicked
  • Is there value in propTypeing repeat required properties at every level of nested components?
  • Call function if variable does not exist in a filter
  • localStorage value doesn't get updated automatically
  • React not updating state with setState correctly inside promises
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com