React native 如何使用react钩子只需一行代码就可以获取所有状态数据?
我最近只是试着用react hook 如果我扩展Componet,我会这样声明我的状态:React native 如何使用react钩子只需一行代码就可以获取所有状态数据?,react-native,react-hooks,React Native,React Hooks,我最近只是试着用react hook 如果我扩展Componet,我会这样声明我的状态: constructor(props) { super(props); // dummy state this.state = { focus: false name: 'test' image: '' avatarSource: '', imageBase: 'sfsafsfasf', imageLoade
constructor(props) {
super(props);
// dummy state
this.state = {
focus: false
name: 'test'
image: ''
avatarSource: '',
imageBase: 'sfsafsfasf',
imageLoader: null,
}
如果我想发送所有状态值,我将使用…this.state
就像:
sendStateFunction(...this.state);
如果我使用react hook,我会这样声明我的状态:
const test = () => {
const [name, setName] = useState('');
const [image, setImage] = useState('');
const [sex, setSex] = useState(0);
const [id, setId] = useState('');
}
但我不知道如何发送所有状态值,只需使用一行代码:
sendStateFunction(); // What should I type the arguments ?
React允许您使用以下钩子定义具有多个子值的状态对象:
function DummyComponent(){
const [state, setState] = useState({ focus: false,
name: 'test',
image: '',
avatarSource: '',
imageBase: 'sfsafsfasf',
imageLoader: null,})
// You can update it like that:
return <Button onClick={()=>setState(prevState=>{return {...prevState, newKey:1}})}>Click</Button>
}
函数DummyComponent(){
const[state,setState]=useState({focus:false,
名称:'测试',
图像:“”,
avatarSource:“”,
imageBase:'sfasf',
imageLoader:null,})
//您可以这样更新它:
return setState(prevState=>{return{…prevState,newKey:1}}}>单击
}
或者,您可以使用钩子useReducer使用减速器。
所有这些问题都在React hooks中进行了说明。React允许您使用如下钩子定义具有多个子值的状态对象:
function DummyComponent(){
const [state, setState] = useState({ focus: false,
name: 'test',
image: '',
avatarSource: '',
imageBase: 'sfsafsfasf',
imageLoader: null,})
// You can update it like that:
return <Button onClick={()=>setState(prevState=>{return {...prevState, newKey:1}})}>Click</Button>
}
函数DummyComponent(){
const[state,setState]=useState({focus:false,
名称:'测试',
图像:“”,
avatarSource:“”,
imageBase:'sfasf',
imageLoader:null,})
//您可以这样更新它:
return setState(prevState=>{return{…prevState,newKey:1}}}>单击
}
或者,您可以使用钩子useReducer使用减速器。
所有这些问题都在React HOOK中说明谢谢。所以我可以在你的例子中使用类似于
…state
,对吗?谢谢。所以我可以在你的例子中使用类似于…state
,对吗?