【前言】
最近工作不順到覺得很挫折、壓力也很大,我覺得Redux跟我不是好朋友了。這不是我想的這樣啊(哭,繼續加油對自己打氣吧!
【正文】
昨天把container component
做完了,今天就比較簡單地把剩下的component接起來吧!
import React from 'react';
import { connect } from 'react-redux';
import { addTodo } from '../../actions';
class AddTodoComponent extends React.Component {
constructor(props) {
super(props);
this.inputRef = React.createRef();
}
render() {
const { dispatch } = this.props;
return(
<div>
<form onSubmit={e => {
e.preventDefault()
if (!this.inputRef.current.value.trim()) {
return
}
dispatch(addTodo(this.inputRef.current.value))
this.inputRef.current.value = ''
}}>
<input ref={this.inputRef} />
<button type="submit">Add Todo</button>
</form>
</div>
)
}
}
const AddTodo = connect()(AddTodoComponent);
export default AddTodo;
import React from 'react';
import { render } from 'react-dom';
// react-redux的Provider可以讓我們將store裡面的值傳給所有container component
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import todoApp from './Redux/reducers';
import App from './Redux/components/presentional/App.js';
const store = createStore(todoApp);
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)