Recoil_React
15632 ワード
定義#テイギ#
//atom 함수를 사용해서 생성
const fontSizeState = atom({
key: 'fontSizeState',
default: 14,
})
const fontSizeLabelState = selector({
key: 'fontSizeLabelState',
//get속성은 계산될 함수
get: ({get}) => {
const fontSize = get(fontSizeState);
const unit = 'px';
return `${fontSize}${unit}`;
},
});
const FontButton = () => {
const [fontSize, setFontSize] = useRecoilState(fontSizeState);
const fontSizeLabel = useRecoilValue(fontSizeLabelState);
return (
<>
<div> font Size: ${fontSizeLabel}</div>
<button onClick={() => setFontSize((size) => size + 1)} style={{fontSize}}>
Enter
</button>
</>
);
}
両親の木に<RecoilRoot></RecoilRoot>
で包むatoms
selecters
TodoList
function TodoItem({item}) {
const [todoList, setTodoList] = useRecoilState(todoListState);
const index = todoList.findIndex((listItem) => listItem === item);
const editItemText = ({target: {value}}) => {
const newList = replaceItemAtIndex(todoList, index, {
...item,
text: value,
});
setTodoList(newList);
};
const toggleItemCompletion = () => {
const newList = replaceItemAtIndex(todoList, index, {
...item,
isComplete: !item.isComplete,
});
setTodoList(newList);
};
const deleteItem = () => {
const newList = removeItemAtIndex(todoList, index);
setTodoList(newList);
};
return (
<div>
<input type="text" value={item.text} onChange={editItemText} />
<input
type="checkbox"
checked={item.isComplete}
onChange={toggleItemCompletion}
/>
<button onClick={deleteItem}>X</button>
</div>
);
}
function replaceItemAtIndex(arr, index, newValue) {
return [...arr.slice(0, index), newValue, ...arr.slice(index + 1)];
}
function removeItemAtIndex(arr, index) {
return [...arr.slice(0, index), ...arr.slice(index + 1)];
}
Reference
この問題について(Recoil_React), 我々は、より多くの情報をここで見つけました https://velog.io/@suminllll/RecoilReactテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol