简体   繁体   English

状态更新后,React不重新发送DOM

[英]React not re-rending DOM after state update

I'm having an issue re-rendering the DOM after a state change, Here is the code for the component: 状态更改后,我在重新渲染DOM时遇到问题,这是该组件的代码:

<ul>
    { this.state.musicLibraryItems }
</ul>

I have a filter function that takes in a value from a text input and and filters the musicLibraryItems based on if the title has the value from the text input, I am doing that in the following function: 我有一个过滤器函数,该函数从文本输入中获取一个值, musicLibraryItems根据标题是否具有文本输入中的值来过滤musicLibraryItems ,我在以下函数中执行此操作:

filterMusicLibrary(value) {
    let musicLibraryItems = [];
    this.state.musicGet.map(music => {
        if(music.title.includes(value)) {
            console.log(music.title)
            musicLibraryItems.push(
                <MusicLibraryItem key= {music.id} music={music} history={this.props.history}/>
            );
        }

    });
    console.log(musicLibraryItems, this.state.musicLibraryItems)

    this.setState((prevState) => {
        return {
            musicLibraryItems: musicLibraryItems
            })
        }
    })

}

I think the problem is that I am not changing the reference to the state variable so it isn't realizing that the state has been changed. 我认为问题在于,我没有更改对状态变量的引用,因此未意识到状态已更改。 I was wondering what the best way to mutate the state variable so that the values in the musicLibraryItems is put into this.state.musicLibraryItems so that it can be rendered on the page. 我想知道改变状态变量的最佳方法是什么,以便将musicLibraryItems中的值放入this.state.musicLibraryItems以便可以在页面上呈现它。

you could try 你可以尝试

<ul>
  { this.filterMusicLibrary() }
</ul>

and change the filter to not change the state 并更改过滤器以不更改状态

filterMusicLibrary() {
    let musicLibraryItems = [];
    this.state.musicGet.map(music => {
        if(music.title.includes(this.state.title)) {
            console.log(music.title);
            musicLibraryItems.push(
                <MusicLibraryItem key= {music.id} music={music} history={this.props.history}/>
            );
        }

    });
    console.log(musicLibraryItems, this.state.musicLibraryItems);
    return musicLibraryItems;
}

You shouldn't store jsx elements on your state. 您不应该在状态上存储jsx元素。 If you are concerned about re-calculation of the list, try reselect 如果您担心列表的重新计算,请尝试重新选择

More like what @Murilo already mentioned, for filters you wouldnt want to add them to state, is more calculated state...just added a working sample 就像@Murilo已经提到的那样,对于过滤器,您不想将其添加到状态,而是更多的计算状态...只是添加了一个工作示例

 const MusicLibraryItem = ({music}) => { return <li>{music.title}</li> } const lotMusic = [{title: 'Awesome', id: '12121'}, {title: 'Kitchen', id: '121'}, {title: 'Golden', id: '21'}, {title: 'Beach', id: '121'}]; class App extends React.Component{ constructor(){ super() this.state = {musicGet: lotMusic,filterValue: ''} this.updateFilter = this.updateFilter.bind(this); } updateFilter(event) { const name = event.target.name; this.setState({[name]: event.target.value}); } filterMusicLibrary(value) { let musicLibraryItems = []; this.state.musicGet.map(music => { if(music.title.includes(value)) { console.log(music.title) musicLibraryItems.push( <MusicLibraryItem key= {music.id} music={music} history={this.props.history}/> ); } }); return musicLibraryItems; } render(){ return ( <div> <h2>Test </h2> <input type="text" onChange={this.updateFilter} name="filterValue" value={this.state.filterValue}/> <ul>{this.filterMusicLibrary(this.state.filterValue)}</ul> </div> ) } } ReactDOM.render(<App/>, document.getElementById('app')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM