I am tring to show todo list with load more option. I am appling limit.Limit is apply to list.But when i add loadmore()function. then i get error this.state.limit is null Wher i am wrong.Any one can suggest me. here is my code todoList.jsx
var TodoList=React.createClass({
render:function(){
var {todos}=this.props;
var limit = 5;
function onLoadMore() {
this.setState({
limit: this.state.limit + 5
});
}
var renderTodos=()=>{
return todos.slice(0,this.state.limit).map((todo)=>{
return(
<Todo key={todo.todo_id}{...todo} onToggle={this.props.onToggle}/>
);
});
};
return(
<div>
{renderTodos()}
<a href="#" onClick={this.onLoadMore}>Load</a>
</div>
)
}
});
module.exports=TodoList;
This is witout button click.
As you all know react components has a function
componentDidMount()
which gets called automatically when the template of that component is rendered into the DOM. And I have used the same function to add the event listener for scroll into our div iScroll. ThescrollTop
property of the element will find the scroll position and add it with theclientHeight
property. Next, the if condition will check the addition of these two properties is greater or equal to the scroll-bar height or not. If the condition is true theloadMoreItems
function will run.This is example
Changes:
1. First define the
limit
instate
variable by usinggetInitialState
method, you didn't define the limit, that's whythis.state.limit
isnull
.2. Define all the
functions
outside of therender
method.3.
Arrow function
withrenderTodos
is not required.4. Use
this
keyword to call therenderTodos
method like this:Write it like this: