I'm new to React and I'm trying to store a value (tweets), even when the page refreshes, so it won't update again because it takes a while. My code basically gets the value from my db in the backend and saves it in tweets and then creates a table with the data. The question is, how do I store this value so I won't have to get it every refresh from the db?
export default props => {
const [tweets, updateTweets] = useState([]);
const [text, updateText] = useState('');
const getList = () => {
axios.get("http://localhost:5000/api/tweet/list")
.then((response) => {
updateTweets(response.data)
})
}
const getName = (parameter) => {
axios.get("http://localhost:5000/api/tweet/name?target="+parameter)
.then((response) => {
updateTweets(response.data)
})
}
// load table data
useEffect(() => {
getList()
}, [])
console.log(tweets)
const ReactTable = (props) => {
return <table border={2} cellPadding={5}>
<tr>
<th>Target</th>
<th>Insult</th>
<th>Tweet</th>
</tr>
<tbody>
{
props.map(function(item) {
return(<tr>
<td>{item.target}</td>
<td>{item.insult}</td>
<td>{item.tweet}</td>
</tr>)
})
}
</tbody>
</table>
}
return (
<div className={"table-div"}>
<label className={"label"}>
Target:
<input type={"text"} value={text} onChange={e => updateText(e.target.value)} />
</label>
<input type={"submit"} value={"Search"} onClick={() => getName(text)} />
{ReactTable(tweets)}
</div>
)
}
question from:
https://stackoverflow.com/questions/66064103/store-array-even-the-page-refreshes-with-hooks-react 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…