How does one go about dynamically rendering a stateful component that takes in props that can change after render? I have tried storing components in state and then mapping out the state however the components do not update when the props passed into them has changed.
Example:
export default App(props){
const [name, setName] = useState(null)
const [articles, setArticles] = useState([])
return (
{articles.map((article) => {article})}
<input type="text" value={name} onChange={e => setName(e.target.value)}/>
<button type="button" onClick={() => setArticles([...articles, <Article name={name}/>])}}>Add Article</button>
)
}
export default Article(props){
return (
<p>props.name</p>
)
}
In my example, when I update the name state by changing the input field, none of the rendered articles update to reflect this change, only new additions when the add article button is pressed after the name has been changed, I want them to update as the state changes, how do I do this?
question from:
https://stackoverflow.com/questions/65541050/how-to-dynamically-render-a-stateful-component-in-react 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…