我正在構建一個基本的React應用程序,該應用程序呈現數組中書籍的列表,並具有將另一個書籍對象推送到數組的表單。我在考慮修改book數組是對道具的一種改變,這會導致Post
組件重新渲染,但是我不能讓Post
重新渲染,除非我強制使用鏈接和導航回來手動強制它。我相信我已經附加了所有相關的組件以及書籍數組。我使用create-react-app來設置React環境。 React版本 - 15.4.1。非常感謝您提供的任何幫助!在基本的React應用程序中重新渲染組件的問題
編輯: 我已經重構了一點,創造了一個GitHub庫,使一切更清晰。
import React from 'react'
import ReactDOM from 'react-dom'
import {Router, Route, IndexRoute, hashHistory} from 'react-router'
import App from './App'
import Display from './Display'
import Content from './Content'
//stylesheet
import './index.css'
//import posts array
import postsArray from './postsArray'
ReactDOM.render(
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Display} entries={postsArray}></IndexRoute>
<Route path="view/:id" component={Content} entries={postsArray}></Route>
</Route>
</Router>,
document.getElementById('root')
);
import React, {Component} from 'react'
import Post from './Post'
class Display extends Component {
constructor(props) {
super(props)
this.handleSubmit = this.handleSubmit.bind(this)
}
handleSubmit(event) {
this.props.route.entries.push({
title: this.titleInput.value,
author: this.authorInput.value,
body: this.bodyText.value
})
event.preventDefault()
}
render() {
return(
<div>
<Post entries={this.props.route.entries}/><br /> <br />
<form onSubmit={this.handleSubmit}>
<label>
Title:<br />
<input type={"text"} ref={(titleInput) => this.titleInput = titleInput} className="input" />
</label><br />
<label>
Author:<br />
<input type="text" ref={(authorInput) => this.authorInput = authorInput} className="input" />
</label> <br />
<label>
Body:<br />
<textarea ref={(bodyText) => this.bodyText = bodyText} className="textAreaInput"/>
</label> <br />
<input type="submit" value="Submit" />
</form>
</div>
)
}
}
export default Display
import React, {Component} from 'react'
import {Link} from 'react-router'
class Post extends Component {
constructor(props) {
super(props)
console.log("insinde post initial", this.props.entries)
}
render() {
return (
<div className="postsWrapper">
{this.props.entries.map((entry, index) => (
<div key={index} className="divMargin">
<Link to={"view/" + index } className="postLink">
<h1 className="postH titleFont">{entry.title}</h1>
<h2 className="authorFont authorMargin">{entry.author}</h2>
</Link>
</div>
))}
</div>
)
}
}
export default Post
var posts = [
{
title: "A Walk in the Woods",
author: "Bill Bryson",
body: "A very enjoyable book!"
},
{
title: "Bridge Over The River Kwai",
author: "Pierre Boulle",
body: "I have never read this book. Not ever."
},
{
title: "Do Not Sell at Any Price",
author: "Amanda Petrusich",
body: "Will you please please please give me this book when you're done?"
},
{
title: "Just Kids",
author: "Patti Smith",
body: "This is a national book award winner! Wow!"
}
]
export default posts
在路由器上使用「條目」對我來說看起來有點不尋常。你能否進一步解釋爲什麼你想以這種方式填充帖子數組? –
而且,由於您正在記錄帖子數組,您的數組道具可以在Post組件中成功更新嗎? –
我有點擅自決定從路由器傳遞陣列作爲道具。這可能不是最好的決定,但即使我通過顯示組件中的道具作爲道具,也會遇到同樣的問題。另外,Post組件實際上只負責渲染每個Post,並且這些內容與更新發生的形式沒有關係,但是我覺得它應該仍然會因道具變化而重新渲染。 –