-
Notifications
You must be signed in to change notification settings - Fork 49.8k
Closed
Labels
Resolution: StaleAutomatically closed due to inactivityAutomatically closed due to inactivity
Description
This is a bug I think.
As the title says, React didn't help me to merge the update requests.
const Player = ({ }) => {
const [name, setName] = useState('John Higgins',)
const [age, setAge] = useState(47)
const nextPlayer = () => {
setName('Mark Selby')
setAge(34)
}
console.log('render.')
return <p>
This player's name is {name}, {age} years.
<button type="button" onClick={nextPlayer}>next</button>
</p>
}After the button clicked, it outputs:
>> render.
>> render.But in fact, I got three.
>> render.
>> render.
>> render.It doesn't always be.
The actual code
const List = ({}) => {
const [items, setItems] = useState<ListItem[]>([])
const [tick, setTick] = useState(0)
const [page, setPage] = useState(1)
// # effect 001
useEffect(() => {
const refresh = tick > 1
load({ refresh, page })
.then(d => {
if (refresh) setItems(d) // replace
else setItems([...items, ...d]) // append
})
}, [tick, page])
// # effect 002
useEffect(() => {
testingScrollToBottom(() => {
setTick(0) // makes effect 001 applied.
setPage(page + 1) // makes effect 001 applied again.
})
return () => {
stopTestingScrollToBottom()
}
}, [page])
return (
<div>
<button
onClick={() => {
setTick(tick + 1)
setPage(1)
}}>
Refresh
</button>
{ items.map(item => <Item item={item}/>) }
</div>
)
}Function testingScrollToBottom is where the things happened.
Here is what I did to avoid that:
By the way, testingScrollToBottom uses setTimeout.
amitnovick
Metadata
Metadata
Assignees
Labels
Resolution: StaleAutomatically closed due to inactivityAutomatically closed due to inactivity
