简体   繁体   English

通过将redux状态传递给react中的动态react-pose道具来移动div

[英]Moving div by passing redux state to dynamic react-pose props in react

I've cobbled together this little sandbox to demonstrate: https://codesandbox.io/s/64xv97y45n 我将这个小沙盒拼凑起来进行演示: https : //codesandbox.io/s/64xv97y45n

The purple div in the upper left hand corner is supposed to move as letters are typed. 左上角的紫色div应该随着键入字母而移动。 When a letter is typed, the currIndex (the currently active box) value on the redux store is incremented or decremented accordingly. 键入字母后,redux存储上的currIndex(当前活动的框)值将相应地增加或减少。 The reducer then uses currIndex to compute the currentCoords or the div's new absolute position (for the purposes of attached sandbox, 'slightly more to the right'). 然后,reducer使用currIndex来计算currentCoords或div的新绝对位置(出于附加沙箱的目的,“稍微向右移动”)。 The currentCoords store property is then passed on as a prop to control the dynamic pose of the purple div. 然后将currentCoords存储属性作为道具传递,以控制紫色div的动态姿势。 But the div refuses to update its pose. 但是div拒绝更新其姿势。 Redux DevTools tells me currentCoords is updating properly, or at least well enough for it to move a little. Redux DevTools告诉我currentCoords正在正确更新,或者至少更新得足够好,可以稍稍移动一下。 What gives? 是什么赋予了?

Relevant logic: 相关逻辑:

 const reducer = (state = initState, action) => {
      switch (action.type) {
        case "KEYPRESS":
          return {
            ...state,
            number: [...state.number, action.key],
            currIndex: ++state.currIndex,
            currentCoords: state.boxCoords[state.currIndex]
          };

<SNIP>

const Cursor = posed.div({
  visible: {
    opacity: 1,
    x: ({ xPos }) => xPos,
    y: ({ yPos }) => yPos
  },

  hidden: {
    opacity: 0
  }
});

<SNIP>



<Cursor
            className="cursor"
            initialPose="visible"
            xPos={this.props.currentCoords.x}
            yPos={this.props.currentCoords.y}
          />

If you want to transition your posed element without changing the current pose you need to pass a poseKey to react on. 如果要在不更改当前pose情况下转换posed元素,则需要传递poseKey来做出反应。 Also according to documentation of initialPose property: 另外根据initialPose属性的文档:

Once the component mounts, it will transition from this pose into pose . 组件安装后,它将从该位姿转换为pose

That is why have must pass pose property to the posed component otherwise initialPose will be reset. 这就是为什么必须将pose属性传递给posed组件的原因,否则initialPose将被重置。 So basically <Cursor> component should be rendered like this: 因此,基本上<Cursor>组件应如下所示:

<Cursor
   className="cursor"
   initialPose="visible"
   pose="visible"                        // you can hold the pose in your state and use it here like this.state.pose
   poseKey={this.props.currentCoords.x}
   xPos={this.props.currentCoords.x}
   yPos={this.props.currentCoords.y}
/>

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

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