Pass from Parent Component to Child with useEffect:
in /twittme-web/src/tweets/components.js ```javascript export function TweetsComponent(props) {
const textAreaRef = React.createRef()
const [newTweets, setNewTweets] = useState([]) //new
const handleSubmit = (event) => { //new
event.preventDefault()const newVal = textAreaRef.current.valuelet tempNewTweets = [...newTweets]tempNewTweets.unshift({ //put it to the beginningcontent: newVal,likes: 0,id: 12313})setNewTweets(tempNewTweets) //newtextAreaRef.current = '' //after the submission, clear the textarea
} return
}<div className='col-12 mb-3'><form onSubmit={handleSubmit}><textarea ref={textAreaRef} required={true} className='form-control' name='tweet'></textarea><button type='submit' className='btn btn-primary my-3'>Tweet</button></form></div><TweetsList newTweets={newTweets}/> <!--new-->
export function TweetsList(props) { //const [tweets, setTweets] = useState([]) const [tweetsInit, setTweetsInit] = useState([]) //rename console.log(props.newTweets) //new, here newTweets is returned above
useEffect(() => {const myCallback = (response, status) => {if (status === 200){//setTweets(response)setTweetsInit(response) //rename} else {alert("There was an error")}}loadTweets(myCallback)}, [])
//return tweets.map((item, index) => {
return tweetsInit.map((item, index) => { //rename
return
refresh and tweet something:<br /><br />and the console log shows the information about this temporary tweet (but it isn't actually updating something)- setup the content of the newTweet above and push it to the list in TweetsList```javascriptexport function TweetsList(props) {const [tweetsInit, setTweetsInit] = useState([])//console.log(props.newTweets)const [tweets, setTweets] = useState([])useEffect(() => {const final = [...props.newTweets].concat(tweetsInit) //[...content] means a new list with the contentif(final.length !== tweets.length) {setTweets(final)}}, [props.newTweets, tweets, tweetsInit])useEffect(() => {const myCallback = (response, status) => {if (status === 200){setTweetsInit(response)} else {alert("There was an error")}}loadTweets(myCallback)}, [])//return tweetsInit.map((item, index) => {return tweets.map((item, index) => {return <Tweet tweet={item} className='my-5 py-5 border bg-white text-dark' key={`${index}-{item.id}`} />})}
refresh and send
works(id=12313), but it’s still a temporary tweet.
