[英]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.