标签: setstate

更改 Scaffold.body 值后如何在颤动中隐藏抽屉

我正在使用此问题中的方法来更改颤振中 Scaffold 的主体:

Flutter Drawer Widget - 更改 Scaffold.body 内容

所描述的方法非常有效。现在我只想在用户点击其中一个项目后自动关闭抽屉。

我尝试使用 Navigator.pop() 方法,但它会弹出整个屏幕,而不仅仅是抽屉。它让我看到一个全黑的屏幕。

有什么建议?

scaffold drawer setstate dart flutter

8
推荐指数
2
解决办法
1万
查看次数

如何在反应测试库中设置状态

概述:

  • 之前用 Enzyme 测试我重构过脚本测试,现在想用@testing-library/react

问题:

  • 我在@testing-library/react 中找不到 setState 的解决方案

setstate react-testing-library

8
推荐指数
1
解决办法
1万
查看次数

异步设置ReactJS状态

如果你执行了一个异步操作来更新状态componentWillMount(比如说文档),但在异步调用完成之前卸载了组件(用户导航),你最终会尝试异步回调试图在现在卸载的组件,和

"不变违规:replaceState(...):只能更新已安装或安装的组件."

错误.

最好的方法是什么?

谢谢.

javascript asynchronous setstate reactjs

7
推荐指数
2
解决办法
781
查看次数

ReactJS并发SetState竞争条件

我有这样的组件结构

<A>
    <B>
        <C/>
        <C/>
    </B>
    <D>
       <E/>
       <E/>
    </D>
</A>
Run Code Online (Sandbox Code Playgroud)

想法是对块E中的组件的操作由组件A的功能处理为A的状态,而不是作为道具传递给B和C. 我知道,更好的方法是使用Flux,pubsub-js或其他Store-message系统,但希望如果有人可以解释为什么正确到我理解的解决方案不起作用.

从组件E的多个实例中单独调用组件A的此函数会导致竞争条件,只有一个状态更改(而不是每个函数调用提供更改)

updateState(value,index){
   this.setState(update(this.state, {
        a: {
          [index]: {
            b: {
             $set: value
            }
          }
        }
    })
);
}
Run Code Online (Sandbox Code Playgroud)

功能更新来自

import update from 'react/lib/update';
Run Code Online (Sandbox Code Playgroud)

反对ReactJS的错误解决方案推荐的做法,但效果很好:

updateState(value,index){
   this.state.a[index].b=value;
   this.forceUpdate();
);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

这是一个错误,多个simalteneous setState调用竞争条件,或者我做错了什么而没有修改它?

race-condition setstate reactjs

7
推荐指数
2
解决办法
6234
查看次数

在渲染容器之前执行react redux调度操作

我是React-redux应用程序开发的新手,我试图了解如何在页面加载后立即发送另一个操作.以下是我的容器代码.我正在使用这个(https://github.com/jpsierens/webpack-react-redux)样板.

let locationSearch;

const ActivationPage = ({activateUser}) => {
    return (
        <div className="container">
          <h2>Activation Required</h2>
          <p>An Activation Email was sent to your email address. Please check your inbox to find the activation link</p>
          { activateUser() }
        </div>
    );
};


ActivationPage.propTypes = {
    activateUser: PropTypes.func
};


const mapStateToProps = (state) => {
    return {
        message: state.message,
        currentUser: state.currentUser,
        request: state.request
    };
};

const mapDispatchToProps = (dispatch) => {
    return {
        activateUser: () => {
            console.log(location);
            if (location.search !== '') …
Run Code Online (Sandbox Code Playgroud)

javascript render setstate reactjs redux

7
推荐指数
1
解决办法
8630
查看次数

React中对象数组的SetState

好的,所以我很沮丧找到合适的解决方案,所以我在这里发布问题.给出答案对我有很大的帮助,因为我被困住了!

状态树看起来像这样

this.state = {
      itemList : [{
                    _id : 1234,
                   description : 'This the description',
                   amount : 100
                    }, {
                    _id : 1234,
                   description : 'This the description',
                   amount : 100
                    }],
     }
Run Code Online (Sandbox Code Playgroud)

问题是:

  1. 无法根据_id更新数组的Object中的任何特定键
  2. 以前的状态应该保持不变

immutability setstate reactjs

7
推荐指数
1
解决办法
9414
查看次数

我们可以将 setState 作为 props 从一个组件传递到另一个组件并在 React 中从子组件更改父状态吗?

 class App extends Component {
  constructor() {
    super();
    this.state = {
      name: 'React'
    };
    this.setState=this.setState.bind(this)
  }

  render() {
    return (
      <div>
        <Hello name={this.state.name} />
        <p>
          Start editing to see some magic happen :)
        </p>
        <Child {...this}/>
      </div>
    );
  }
}

child Component
var Child=(self)=>{
  return(
    <button  onClick={()=>{
      self .setState({
        name:"viswa"
      })
    }}>Click </button>
  )
Run Code Online (Sandbox Code Playgroud)

在这里我绑定 setState 函数并将其作为道具发送到子组件。这将改变父组件的状态。这是正确的方法吗?

javascript function setstate reactjs react-props

7
推荐指数
3
解决办法
1万
查看次数

React Hooks:在多个连续的 setState 调用上跳过重新渲染

假设我有以下代码:(太冗长了)

function usePolicyFormRequirements(policy) {
  const [addresses, setAddresses] = React.useState([]);
  const [pools, setPools] = React.useState([]);
  const [schedules, setSchedules] = React.useState([]);
  const [services, setServices] = React.useState([]);
  const [tunnels, setTunnels] = React.useState([]);
  const [zones, setZones] = React.useState([]);
  const [groups, setGroups] = React.useState([]);
  const [advancedServices, setAdvancedServices] = React.useState([]);
  const [profiles, setProfiles] = React.useState([]);

  React.useEffect(() => {
    policiesService
      .getPolicyFormRequirements(policy)
      .then(
        ({
          addresses,
          pools,
          schedules,
          services,
          tunnels,
          zones,
          groups,
          advancedServices,
          profiles,
        }) => {
          setAddresses(addresses);
          setPools(pools);
          setSchedules(schedules);
          setServices(services);
          setTunnels(tunnels);
          setZones(zones);
          setGroups(groups);
          setAdvancedServices(advancedServices);
          setProfiles(profiles);
        }
      );
  }, …
Run Code Online (Sandbox Code Playgroud)

performance setstate reactjs react-state-management react-hooks

7
推荐指数
2
解决办法
6568
查看次数

Antd 通知出现两次?

我正在使用 antd 通知组件,如下所示

        import { notification } from "antd";

        const Notification = ({ msg, type, clearMsg }) => {
          return (
            <div>
              {notification[type]({
                description: msg,
              })}
              {clearMsg()}
            </div>
          );
        };

        export default Notification;
Run Code Online (Sandbox Code Playgroud)

我只是在需要弹出通知的任何地方使用它。例如API响应失败后:

          const onSubmit = async (e) => {
            try {
              const res = await login(data);
              if (res.message) {
                setError({ state: true, msg: res.message });
              }
            } catch (err) {
              console.log(err);
            }
          };
Run Code Online (Sandbox Code Playgroud)

然后根据错误状态,我在返回正文中显示通知,如下所示:

          { error.state ?
            <Notification 
               type="error" 
               msg="some msg" 
               clearMsg={() => setError({state: false, msg: ""})} : …
Run Code Online (Sandbox Code Playgroud)

setstate reactjs antd

7
推荐指数
2
解决办法
5342
查看次数

当我在函数体中设置状态时,为什么 React 会变成 Infinite?

如果我们用相同的值设置状态组件将不会重新渲染,但当我在函数体中设置状态时它不适用。

例如,如果我在按钮单击和单击按钮上设置相同的状态,则组件不会在按钮单击时重新渲染

function Test1() {
  const [name, setName] = useState("Shiva");
  const onButtonClick = () => {
    console.log("Clicked");
    setName("Shiva");
  };
  console.log("Redering");
  return (
    <div>
      <span>My name is {name}</span>
      <button onClick={onButtonClick}>Click Me</button>
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

But, when I set the same state before the return statement React goes infinite renderings

function Test2() {
  const [name, setName] = useState("Shiva");
  // ... come stuff
  setName("Shiva");
  console.log("Rendering");
  return (
    <div>
      <span>My name is {name}</span>
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

What actually happening internally?

javascript frontend setstate reactjs react-hooks

7
推荐指数
2
解决办法
1247
查看次数