简体   繁体   English

我正在尝试在Codepen中构建一个React-Redux'Markdown Previewer',但是在弄清楚应该做什么方面有很多麻烦

[英]I'm trying to build a React-Redux 'Markdown Previewer' in codepen, but am having lots of trouble figuring out what is supposed to be doing what

I'm struggling quite a bit with React-Redux. 我在React-Redux上挣扎了很多。 I watched Dan Abramovs intro to redux 3 times, followed along with the coding 1 of those times, read the Redux documentation twice, and went through the React and Redux sections of freeCodeCamp 3 times each but I can't seem to wrap my head around it. 我观看了Dan Abramovs关于redux的3次介绍,接着是其中1次的编码,两次阅读了Redux文档,并分别浏览了freeCodeCamp的React和Redux部分3次,但我似乎无法全神贯注它。 I get the very basics, but when it starts to get more complicated I get lost. 我掌握了非常基本的知识,但是当它变得越来越复杂时,我就会迷路。 I decided to try and build out the markdown previewer challenge to see if I could learn as I go, but I can't get the preview field to update as I update the editor field. 我决定尝试构建markdown预览器挑战,以查看我是否可以边走边学,但在更新编辑器字段时无法更新预览字段。 This is what I have so far, I could get it to display basic HTML like "This is the header" a text box that says "This is the editor" and basic stuff like that, but now the next step I'm working on is getting the editor field to dynamically update the preview field. 这就是到目前为止,我可以显示基本的HTML,例如“ This is the header”,一个文本框显示“ This is the editor”和类似的基本内容,但是现在我正在下一步获取编辑器字段以动态更新预览字段。 As soon as I started trying to do that, everything went blank and the console stopped giving me any feedback. 当我开始尝试这样做时,一切都变得一片空白,控制台也停止提供任何反馈。

const { Component } = React;
const { Provider, connect } = ReactRedux;


const previewReducer = (
  state='DEFAULT_STATE',
  action
) = {

};

const Header = () => (
  <p> This is the header </p>
);

const Editor = () => (
  <textarea id='editor' value={this.props.data.value}
    onChange={event => previewReducer(event)} />
);

const Previewer = () => (
  <p id='preview'> This is the Previewer </p>
);

const Footer = () => (
  <p> This is the Footer </p>
);

const MarkDownApp = () => (
  <div>
    <Header />
    <Editor />
    <Previewer />
    <Footer />
  </div>
);

const { createStore } = Redux;

ReactDOM.render(
  <Provider store={createStore(markDownApp)}>
    <MarkDownApp />
  </Provider>,
  document.getElementById('root')
);

I understand well enough to know that what I have right now shouldn't work, but I don't know why or how to fix it. 我非常了解,知道我现在所拥有的应该行不通,但是我不知道为什么或如何解决。 I'm not really sure what I should be doing with my reducer which is why it's empty. 我不太确定我应该如何使用我的减速器,这就是为什么它是空的。

You're calling the reducer directly from your onChange property. 您是直接从onChange属性调用reducer。 Instead you should register the reducer in the store, and dispatch an action on the store, that in turn will use the reducer to update the state in the store. 取而代之的是,您应该在商店中注册化简器,并在商店上分派一个动作,该动作随后将使用化简器更新商店中的状态。

You probably do not want to pass a raw event to dispatch, but something like this could work; 您可能不想传递原始事件进行分派,但是类似的事情可能会起作用。

onChange={event => store.dispatch({ type: 'SOURCE_CHANGED', event: event })}

Reducer registration; 减速机注册;

const store = createStore(previewReducer);

And now to handle the action in the reducer; 现在处理减速器中的操作;

const previewReducer = (state = {}, action) => {
  if (action.type === 'SOURCE_CHANGED') {
    return { source: action.event.target.value };
  }
  return state;
}

And after this, you have to "connect" the components that need to use this source. 然后,您必须“连接”需要使用此源的组件。

But I'm sorry to say that you apparently do not yet understand how redux works, so I recommend you to have a look at the more introductory tutorials. 但是很遗憾,您显然还不了解redux的工作原理,所以我建议您看一下更入门的教程。

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

相关问题 我无法弄清楚我的自定义挂钩中的逻辑 - I’m having trouble figuring out the logic in my custom hook React-Redux不会渲染,如何解决正在发生的事情? - React-Redux will not render, how do I troubleshoot what is going on? 我很难弄清楚我的html和js文件出了什么问题。 我需要数元音 - I am having a hard time figuring out what is wrong with my html and js file. I need to count the vowels 我无法弄清楚如何将Google Geocoder Api与其他API结合使用 - I'm having trouble figuring out how to use the Google Geocoder Api in conjuction with another API 在javascript中,我无法弄清楚如何使正则表达式只替换捕获而不是匹配 - In javascript, I'm having trouble figuring out how to make a regex replace only the capture and not the match 我无法从属性中提取XML数据,我不知道我做错了什么 - I'm having trouble extracting XML data from attributes and I don't know what I'm doing wrong react-redux 中“@@INIT”操作的目的是什么? - What is the purpose of the "@@INIT" action in react-redux? React-Redux中mapStateToProps中的状态参数是什么 - what is state parameter in mapStateToProps in React-Redux 什么是React-Redux环境中的“撕裂”? - What is “Tearing” in the context of the React-Redux? getState() 在 React-Redux 中不起作用的原因是什么? - What is the reason getState() not functioning in React-Redux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM