reactjs - 价值未发送
问题描述
我是redux的新手。我输入了一段简单的代码,它只发送了“Mike”这个名字
但是,当我单击buttonClick()
我创建的函数时.. 也没有发送任何内容,我不确定如何显示发送的值“Mike”,上面写着“嗨,我的名字是...”
下面是我的代码。任何帮助将非常感激
import React from 'react';
import './App.css';
import {changeName} from './reducers.js'
import {createStore} from 'redux';
import {changeNameAction} from './actions'
import {connect} from 'react-redux';
var store = createStore(changeName);
const App = ({buttonClick=f=>f }) => {
return(
<div className="App">
<div> Hi my name is {} </div>
<div><button onClick={()=> buttonClick("Mike")}>Change name</button></div>
</div>
)
};
const mapStateProps = (state) => {
return {
name: state.name
}
}
const mapDispatchToProps = dispatch => {
return {
buttonClick(name){
dispatch(changeNameAction(name))
}
}
}
const Container = connect(mapStateProps,mapDispatchToProps)(App);
export default Container;
解决方案
我现在无法测试你的代码,但你可以试试这个:
// The connected props are part of the arguments
const App = ({ buttonClick = () => {}, name = '' }) => {
return(
<div className="App">
{/* This way you can render the name prop */}
<div> Hi my name is {name}</div>
{/* Execute the function instead of returnning it */}
<div><button onClick={() => { buttonClick("Mike"); }}>Change name</button></div>
</div>
)
};
const mapStateProps = (state) => {
return {
name: state.name
}
}
const mapDispatchToProps = dispatch => {
return {
// Key value pair
buttonClick: (name) => {
dispatch(changeNameAction(name))
}
}
}
希望能帮助到你
推荐阅读
- regex - 如何从成绩单中打印多个时间戳?
- kdb - 如何在 KDB Q 中按日期对列求和?
- azure - 可以将 Docker 可信注册表与 Azure Kubernetes 服务一起使用吗?
- java - 传输文件的 Java 聊天
- c# - 从条码阅读器获取正确的字符,独立于输入法
- python - 仅使用操作名称和服务帐户凭据访问使用 AutoML 批量预测创建的长时间运行的操作
- reactjs - React Fragments 简写 <> 打破了 VSCode 语法高亮
- outlook - VSTO:当单击仅更新表单区域的插件自定义功能区按钮时强制显示保存对话框
- google-apps-script - 优化脚本运行时收集大量电子表格选项卡信息
- google-apps-script - 为什么经常出现“ReferenceError: Drive is not defined”?