简体   繁体   English

如何在没有forceupdate的情况下让反应组件重新渲染?

[英]How to get a react component to rerender without forceupdate?

I'm trying to learn react by building a simple web app where a user can upload an image and apply different effects to them. 我正在尝试通过构建一个简单的Web应用程序来学习反应,用户可以上传图像并对其应用不同的效果。 I hit a bit of a road block. 我打了一个路障。 I'm trying to figure out how to rerender the imagecontainer after the user clicks one of the effects. 我试图弄清楚在用户点击其中一个效果后如何重新渲染图像容器。 I read about forceupdate but it's not recommended to use. 我读到了关于forceupdate的内容,但不建议使用。 Here my code: 这是我的代码:

import React, { Component } from 'react';

import ImageContainer from './components/image_container';
import AppliedContainer from './components/applied_container';
import UnappliedContainer from './components/unapplied_container';

class App extends Component {

constructor(props) {
    super( props );
    this.state = {
        transform :  false  ,
        rotate : false  ,
        scale : false   ,
        opacity : false 
    }

};


render() {
    return (
        <div className="row">
            <ImageContainer onChange={this.state} />
            <UnappliedContainer types={this.state} onSelect={ state => 
     this.setState(state) }/>
            <AppliedContainer types={this.state} onSelect={ state => 
     this.setState(state) }/>             
        </div>
    );
}

}

export default App;

Thanks Guys! 多谢你们!

It depends on what onChange is doing inside of ImageContainer. 这取决于onChange在ImageContainer中所做的事情。 If you want to rerender ImageContainer then onChange inside of ImageContainer could call this.setState to set the state of ImageContainer to its new props. 如果你想重新呈现ImageContainer,那么ImageContainer里面的onChange可以调用this.setState来将ImageContainer的状态设置为它的新道具。

You could also use the componentWillRecieveProps, or componentShouldUpdate lifecycle methods to trigger a rerender. 您还可以使用componentWillRecieveProps或componentShouldUpdate生命周期方法来触发重新渲染。

http://busypeoples.github.io/post/react-component-lifecycle/ http://busypeoples.github.io/post/react-component-lifecycle/

According to React's nature, updating the component without using forceUpdate is changing your state 根据React的性质,在不使用forceUpdate的情况下更新组件会改变您的状态

<ImageContainer onChange={this.state} />

I don't really understand what you pass here as props (onChange), shouldn't it be a function, instead of your state-object? 我真的不明白你在这里传递的是道具(onChange),它不应该是一个函数,而不是你的状态对象吗?

In my opinion, you should create some function in the App component, in that function, you change your state by this.setState({...}) , then you pass this function into your child component as props. 在我看来,你应该在App组件中创建一些函数,在该函数中,你通过this.setState({...})改变你的状态,然后你将这个函数作为props传递给你的子组件。 Besides, please also remember to bind that function into the App component 此外,还请记住将该功能绑定到App组件中

Since you are new to React, would you mind writing other components' code as well, and tell me what you want for the behaviors of all components, so we can find the best way together 由于您是React的新手,您是否也介意编写其他组件的代码,并告诉我您对所有组件的行为有什么要求,以便我们可以一起找到最好的方法

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

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