嗨,我想问一下如何将对象从反应状态复制到另一个临时状态。我这样尝试过:
startEditing() {
this.setState({editMode: true});
//Save schedule before changes
this.setState({oldSchedule: this.state.schedule});
}
cancelEditing(){
this.setState({editMode:false});
//revert changes in schedule
this.setState({schedule:this.state.oldSchedule});
this.setState({oldSchedule:null});
}
我不明白为什么这行不通,但不知道如何正确执行。你能帮我吗?
计划是对象类型
答案 0 :(得分:2)
您可以尝试的最安全的方法是,无需调用多个setState
startEditing() {
this.setState({});
//Save schedule before changes
this.setState({ oldSchedule: { ...this.state.schedule }, editMode: true });
}
cancelEditing() {
this.setState((prevState) => {
return {
editMode: false,
schedule: prevState.oldSchedule,
oldSchedule: null
}
});
}
答案 1 :(得分:1)
如果schedule是一个对象,则应复制该对象而不是对象本身:
startEditing() {
this.setState({editMode: true});
//Save schedule before changes
this.setState({oldSchedule: {...this.state.schedule}});
}
cancelEditing(){
this.setState({editMode:false});
//revert changes in schedule
this.setState({schedule: {...this.state.oldSchedule}});
this.setState({oldSchedule:null});
}
答案 2 :(得分:1)
因为您不复制先前的对象,所以您对其进行另一个引用; 您应该深度复制该对象;一种方法是使用json.parse();
import plotly.figure_factory
def plot_tick(data, ticker):
ts = data[data["ticker"]==ticker].reset_index(drop=True)
fig = plotly.figure_factory.create_candlestick(ts.open, ts.high, ts.low,
ts.close, dates=ts.time)
fig.show()
plot_tick(df_f, 'ticker1')
答案 3 :(得分:1)
您可以尝试一种完全不同的方法-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Raycaster : MonoBehaviour
{
public GameObject cursor;
public Camera cam;
void Update()
{
/// Cast ray
if (Input.GetMouseButtonDown(0))
{
Vector3 cursorPos = cursor.transform.position;
Vector3 rayOrigin = new Vector3(
cursorPos.x,
cursorPos.y,
cam.transform.position.z
);
float cursorRad = cursor.GetComponent<SpriteRenderer>().size.x / 2;
int sealantGuideLayer = 1 << 9;
Vector3 rayDirection = cursorPos - rayOrigin;
RaycastHit2D hit_circle = Physics2D.CircleCast(rayOrigin, cursorRad, rayDirection, 10f, sealantGuideLayer);
Debug.DrawRay(rayOrigin, rayDirection * 10, Color.yellow);
if (hit_circle.collider != null)
{
Debug.Log("-----------------Hit: " + hit_circle.transform.name);
}
else
{
Debug.Log("No hit");
}
}
/// Set cursor pos
Vector3 p = cam.ScreenToWorldPoint(Input.mousePosition);
cursor.transform.position = new Vector3(p.x, p.y, 0f);
}
}